objective c - GCD - How to wait on the main thread for an async callback that is performed on the main queue -
i want perform 2 blocks 1 after other , each on there own performed asynchronously.
for instance
[somemethodwithcompletionhandler:^() { // in completion handler }]; [anothermethodwithcompletionhandler:^ { // else in completion handler after first method completes }];
now, need 'anothermethodwithcompletionhandler
' happen after 'somemethodwithcompletionhandler
' completes (including completion handler)
regularly create dispatch_group
, wait in between 2 methods (i can not nest 2 methods in other completion handler because require lot of code moved)
but problem first completion handler block called in main thread (by method calling block in main thread) can not create dispatch_group
blocking main thread.
so thread state looks this
// main thread here [self dofirstportionofwork]; // main thread here [self dosecondportionofwork]; -(void)dofirstportionofwork { .. lot of stuff happening [somemethodwithcompletionhandler:^{ // main thread }]; // block return here until completion handler finishes } -(void)dosecondportionofwork { ... lot of work here [anothermethodwithcompletionhandler^{ // called main thread }]; // block return here until completion handler finishes }
so how out resorting lot of nested methods , able block main thread until complete?
main thread same main queue
it not possible wait on main thread future work in main thread. blocking future work.
you can this:
[somemethodwithcompletionhandler:^() { // in completion handler [anothermethodwithcompletionhandler:^ { // else in completion handler after first method completes }]; }];
Comments
Post a Comment