ray
ray

Reputation: 2045

dispatch_group_wait doesn't function as expected unless used with NSRunLoop?

I'm trying to figure out what special property NSRunLoop has that causes the following behavior.

First, what I'm trying to do is wait for CLGeocoder to finish executing before moving on. If I use the completion block, the code will look something like this:

if (/* loc has valid coordinates */)
    [gc reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError error){
        //do things
        [gc reverseGeocodeLocation:newloc completionHandler:^(NSArray *placemarks, NSError error){
            //do more things with newloc
            // MOVE TO NEXT VIEW
        }
     }
else if (/*still have to check for newloc*/)
    [gc reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError error){
        //do things
        //MOVE TO NEXT VIEW

Unfortunately the //do things part of those blocks is verbose and it'd be much cleaner if I nested CLGeocoder in its own function, and moved to the next view once I've called it twice.

I found a way to force waiting thanks to the answer here: Waiting for CLGeocoder to finish on concurrent enumeration

So the new method works, but I don't know WHY it works. Here's the code:

dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);

[gc reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError *error){
    //do things
    dispatch_group_leave(group);
}

//this is the confusing part!
while(dispatch_group_wait(group,DISPATCH_TIME_NOW)){
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
                             beforeDate:[NSDate dateWithTimeIntervalSinceNow:1.0f]];
}
dispatch_release(group);

Oddly, if I do the following without the while loop, the app hangs:

dispatch_group_wait(group,DISPATCH_TIME_FOREVER);

Given everything I've read so far, that should work, right?


Even more confusingly, the NSRunLoop is a necessity within the while loop. If I remove it altogether, leaving an empty while loop, the loop will repeat endlessly. Like so:

//this is the confusing part!
while(dispatch_group_wait(group,DISPATCH_TIME_NOW)){
    //without NSRunLoop, this just goes on forever
}

What is NSRunLoop doing that allows the while loop to successfully end?

Upvotes: 3

Views: 1438

Answers (1)

rob mayoff
rob mayoff

Reputation: 385580

The CLGeocoder documentation says that it runs the completion handler block on the main thread. I deduce that you are running your wait-forever or your while(dispatch_group_wait(...)) loop on the main thread. If you are blocking the main thread, the main thread cannot run the completion block.

By calling runMode:beforeDate: in your while loop, you are giving the run loop a chance to run the completion block.

What you are doing is bad. You should not block the main thread, and you should try to avoid running a run loop recursively (because, as you have discovered, it is confusing). The reason CLGeocoder lets you provide a completion block is so that you can set it going and then return to the main run loop. Later, when CLGeocoder runs your completion block, you can use the result in whatever way you need.

Upvotes: 2

Related Questions