The runCriticalSection
method will be called several times, just not at the same time, so I donโt know, this is what you want to achieve.
dispatch_sync
just add the specified block to the serial queue (global default priority queue), so if applicationDidBecomeActive
is run twice in a row, the queue will contain two blocks that will run runCriticalSection
. When the first one starts and its execution ends, the second one starts, so there will be no simultaneous execution of two blocks.
Is this the expected behavior? If so, dispatch_sync
is the way to go.
In addition: if runCriticalSection
is performing a heavy operation, consider dispatch_sync
to block the thread that starts the applicationDidBecomeActive
method (the main one, unless you manually call the method from another thread) until this operation completes.
If you want to avoid this, you should do something like:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self runCriticalSectionOnComplete:^{
dispatch_async
will return as soon as the block is added to the queue, and dispatch_sync
will wait for code to complete inside the block.
source share