ios - If I have a dispatch_sync call followed by a second dispatch call within a dispatch_async block, does it matter if that second call is sync or async? -
this code pretty standard situation i'm having data model potentially slow data retrieval , want update view data once data retrieval finishes.
dispatch_queue_t queue = dispatch_get_global_queue(dispatch_queue_priority_default, 0); dispatch_async(queue, ^{ dispatch_sync(queue, ^{ //get bunch of data } dispatch_sync(dispatch_get_main_queue(), ^{ [viewcontroller tellviewtoreloaddata]; } }
my question here is, make of difference if second dispatch_sync instead dispatch_async?
this understanding of what's happening code above (i'm using opportunity gauge understanding of overall topic):
the outer dispatch_async returns, , block 2 dispatch calls gets put concurrent queue.
at point outer async block execute on random thread, @ point first dispatch_sync gets called , block gets put concurrent queue.
at point inner first sync block execute of data gathering operations on random thread, , when finished first dispatch_sync return , second dispatch_sync called.
the second dispatch_sync gets called, , block code update view gets put main queue. @ point block executes , view gets updated, , second dispatch_sync returns.
the outer block finished executing concurrent queue free possibly push task thread executing outer block.
now, understanding if second dispatch_sync instead dispatch_async call, change thread executing outer block occupied shorter amount of time because doesn't have wait block in main queue finish executing. correct? reasoning seems better second dispatch_sync dispatch_async because thread occupied (probably trivially) shorter amount of time, doesn't matter.
first sync pointless – you're on
queue
.dispatch_async(queue, ^{ //get bunch of data dispatch_sync(dispatch_get_main_queue(), ^{ [viewcontroller tellviewtoreloaddata]; }); });
sync
async
waiting primitive, if was:dispatch_semaphore_t s = dispatch_semaphore_create(0); dispatch_async(queue, ^{ [viewcontroller tellviewtoreloaddata]; dispatch_semaphore_signal(s); }); dispatch_semaphore_wait(s, dispatch_time_forever);
now, understanding if second dispatch_sync instead dispatch_async call, change thread executing outer block occupied shorter amount of time because doesn't have wait block in main queue finish executing. correct?
yes.
by reasoning seems better second dispatch_sync dispatch_async because thread occupied (probably trivially) shorter amount of time, doesn't matter.
it may matter in situations when many threads stuck waiting main thread complete being under stress loads. system need spawn more pthreads continue perform other concurrent tasks , close excessive threads afterwards. can't tell if applies, there no need wait if don't need results.
Comments
Post a Comment