NSMutableDictionary & dispatch_async Native C # objects equivalent or similar

I am new to xamarin and mobile. I am currently migrating my Firebase geology library to xamarin.ios. I came across the following objective-c code and tried to figure out what actually does? and its C # equivalent:

@property (nonatomic, strong) NSMutableDictionary *keyEnteredObservers;

      

...

GFQueryLocationInfo *info = self.locationInfos[key];

[self.keyEnteredObservers enumerateKeysAndObjectsUsingBlock:^(id observerKey,
                                                                      GFQueryResultBlock block,
                                                                      BOOL *stop) {
        dispatch_async(self.geoFire.callbackQueue, ^{         // the callbackQueue is of type @property (nonatomic, strong) dispatch_queue_t callbackQueue;
            block(key, info.location);
        });
    }];

      

for callbackQueue I am currently using BlockingCollection <'Task> type

Any help is greatly appreciated.

+3


source to share


1 answer


A NSMutableDictionary.enumerateKeysAndObjectsUsingBlock

does two main things in this context.

  • It filters the dictionary based on the key. (A Linq Where

    will work as a replacement for C #)

  • Filtered dictionary entries are available to the "block" of code because it is executed asynchronously ( dispatch_async

    ) via GCD (Grand Central Dispatch). So you already use BlockingCollection

    as a pump problem, so the pass line key

    , which is a parameter updateLocationInfo

    and CLLocation

    for your application ...

Note: boolean stop

will cause early exit from the enumerator, so C # break

as a replacement, but it is not used in this context ...



If you are still using NSMutableDictionary

for your observers, you can filter through KeysForObject

. In my version, GeoFire

I used BlockingCollection<Action>

vs. Tasks and added lambdas directly to the work queue.

Something like:

foreach (var info in keyEnteredObservers.KeysForObject(firebaseHandle))
{
    var nonCapturedLocation = info.location.Copy(); // location = CLLocation, do not capture entire dictionary by ref in lambda
    callbackQueue.Add(() =>
    {
        GFQueryResultBlock(key, nonCapturedLocation);
    });
}

      

+1


source







All Articles