The code in your dispatch_sync()
block should indeed finish executing before your barrier block. The same isn't true for the code in the completion block that you pass to -[MKLocalSearch startWithCompletionHandler:]
, however. -startWithCompletionHandler:
is an asynchronous method, and there's a good chance that the search won't complete before the end of the block in which you start it.
Note that the completion block runs on the main thread -- from the docs:
The provided completion handler is always executed on your app’s main thread.
That is, the completion block won't be scheduled on your queue
, but on the main dispatch queue instead. So, even if the completion block were scheduled before your synchronous block completes, it would be scheduled on a different dispatch queue and wouldn't prevent the barrier block from starting.
It looks like your goal is to delay the call to [findMatches notifyNearbyTasks]
until all the searches have completed. One way to do that might be to add each search to a list, and have the completion block check remove the search from the list, check the size of the list, and make the appropriate call when the list is empty.