grand central dispatch - iOS - GCD groups and significant UI delay on finish -
i using gcd run code in background. operation finished in few seconds , in end "done" printed expected. have wait ~5 seconds (simulator) ~1 minute (device) hud hide. might reason such long delay? know processing finished because can see result every element.
mbprogresshud *hud = [mbprogresshud showhudaddedto:self.view animated:yes]; hud.mode = mbprogresshudmodeannulardeterminate; dispatch_group_t group = dispatch_group_create(); dispatch_queue_t queue = dispatch_get_global_queue(dispatch_queue_priority_high,0); nsarray *keys = [nsarray arraywitharray: [codepositions allkeys]]; (id key in keys) { if (key == nil) continue; dispatch_group_async(group, queue, ^{ ... // processing nslog(@"element %d result: %@", element.id, element.result); hud.progress += 1.0/96.0; }); } dispatch_group_notify(group, queue, ^{ nslog(@"done"); [hud hide:yes]; });
thanks time.
you need perform ui updates on main thread. update code follows:
for (id key in keys) { if (key == nil) continue; dispatch_group_async(group, queue, ^{ ... // processing nslog(@"element %d result: %@", element.id, element.result); dispatch_async(dispatch_get_main_queue(), ^{ hud.progress += 1.0/96.0; }); }); } dispatch_group_notify(group, queue, ^{ nslog(@"done"); dispatch_async(dispatch_get_main_queue(), ^{ [hud hide:yes]; }); });
Comments
Post a Comment