multithreading - How to wait for a thread to finish in Objective-C -
i'm trying use method class downloaded somewhere. method executes in background while program execution continues. not want allow program execution continue until method finishes. how do that?
here's way using gcd:
- (void)main { [self dostuffinoperations]; } - (void)dostuffingcd { dispatch_group_t d_group = dispatch_group_create(); dispatch_queue_t bg_queue = dispatch_get_global_queue(dispatch_queue_priority_default, 0); dispatch_group_async(d_group, bg_queue, ^{ [self dosomething:@"a"]; }); dispatch_group_async(d_group, bg_queue, ^{ [self dosomething:@"b"]; }); dispatch_group_async(d_group, bg_queue, ^{ [self dosomething:@"c"]; }); // can synchronously wait on current thread: dispatch_group_wait(d_group, dispatch_time_forever); dispatch_release(d_group); nslog(@"all background tasks done!!"); // **** or **** // if want happen after done: dispatch_group_notify(d_group, dispatch_get_main_queue(), ^{ dispatch_release(d_group); nslog(@"all background tasks done!!"); }); } - (void)dosomething:(id)arg { // whatever want arg here }
Comments
Post a Comment