ios - Nested dispatch_async (dispatch_async inside another dispatch_async) -
how call dispatch_async
calls inside dispatch_async
dispatch_group_t downloadqueue = dispatch_group_create(); dispatch_group_async(downloadqueue,dispatch_get_global_queue(dispatch_queue_priority_high, 0), ^ { nslog(@"main"); [nsthread sleepfortimeinterval:5.0]; nslog(@"end"); [self mymethod]; }); dispatch_group_notify(downloadqueue,dispatch_get_global_queue(dispatch_queue_priority_high,0), ^ { nslog(@"final"); });
and here mymethod :
-(void)mymethod { dispatch_group_t group = dispatch_group_create(); dispatch_group_async(group,dispatch_get_global_queue(dispatch_queue_priority_high, 0), ^ { // block1 nslog(@"block1"); [nsthread sleepfortimeinterval:5.0]; nslog(@"block1 end"); }); dispatch_group_async(group,dispatch_get_global_queue(dispatch_queue_priority_high, 0), ^ { // block2 nslog(@"block2"); [nsthread sleepfortimeinterval:8.0]; nslog(@"block2 end"); }); dispatch_group_notify(group,dispatch_get_global_queue(dispatch_queue_priority_high, 0), ^ { // block3 nslog(@"block3"); }); }
and output :
main
end
block1
block2
final
block1 end
block2 end
block3
but need
main
end
block1
block2
block1 end
block2 end
block3
final
you need notified group of yours mymethod
. yours code be:
dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_high, 0), ^ { nslog(@"main"); [nsthread sleepfortimeinterval:5.0]; nslog(@"end"); dispatch_group_notify([self mymethod],dispatch_get_global_queue(dispatch_queue_priority_high,0), ^ { nslog(@"final"); }); }); -(dispatch_group_t)mymethod{ dispatch_group_t group = dispatch_group_create(); dispatch_group_async(group,dispatch_get_global_queue(dispatch_queue_priority_high, 0), ^ { // block1 nslog(@"block1"); [nsthread sleepfortimeinterval:5.0]; nslog(@"block1 end"); }); dispatch_group_async(group,dispatch_get_global_queue(dispatch_queue_priority_high, 0), ^ { // block2 nslog(@"block2"); [nsthread sleepfortimeinterval:8.0]; nslog(@"block2 end"); }); dispatch_group_notify(group,dispatch_get_global_queue(dispatch_queue_priority_high, 0), ^ { // block3 nslog(@"block3"); }); return group; }
Comments
Post a Comment