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?