Dispatch_group_notify does not wait for one dispatch_group_async

In two parallel background tasks, it is necessary to fix two separate arrays, which must be combined into a dispatch_group_notify block. The problem is that the first block is exceeded, but dispatch_group_notify is exceeded, without waiting for the second background task to complete. The only difference between them is that the first one performs a local search, and the second one makes a remote call to the web service. Anyone tell me why the second jumped?

Edit: I also tried the approach mentioned in https://stackoverflow.com/a/2129609/260 using dispatch_barrier_async, but still the same.

dispatch_group_t taskGroup = dispatch_group_create(); dispatch_queue_t mainQueue = dispatch_get_main_queue(); __block NSArray *localAddresses; __block NSArray *remoteAddresses; //Get local array in the background dispatch_group_async(taskGroup, mainQueue, ^{ //localAddresses is set }); //get remote array from server dispatch_group_async(taskGroup, mainQueue, ^{ [[MDAddressManager instance] searchForPlacesContainingText:query location:alocation completion:^(NSArray* addresses, MDError *error){ //remoteAddresses is set }); //Merge two arrays dispatch_group_notify(taskGroup, mainQueue, ^{ //remoteAddresses and local addresses are merged }); 

And the remote search method is as follows:

 - (void)searchForPlacesContainingText:(NSString *)searchText location:(CLLocation *)alocation completion:(MDAddressManagerBlock)completionBlock { NSDictionary *parameters = [[NSMutableDictionary alloc] init]; [parameters setValue:searchText forKey:@"input"]; [[MDHTTPClient sharedHTTPClient] getPath:@"v1/remotePlaces.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id dict) { if ([MDHTTPClient isResponseValid:dict]) { completionBlock(returnArray, nil); } else { completionBlock(nil, nil); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { EDLog(@"%@", error); completionBlock(nil, [MDError errorAFNetworking:error]); }]; } 
+7
asynchronous ios queue objective-c-blocks grand-central-dispatch
source share
1 answer

This is because your getPath method runs asynchronously. You need him to not leave the group until the completion block is completed. Therefore, instead of dispatch_group_async you need to manually dispatch_group_enter and dispatch_group_leave .

You can change your code:

 dispatch_group_async(taskGroup, mainQueue, ^{ [[MDAddressManager instance] searchForPlacesContainingText:query location:alocation completion:^(NSArray* addresses, MDError *error){ //remoteAddresses is set }]; }); 

To:

 dispatch_group_enter(taskGroup); [[MDAddressManager instance] searchForPlacesContainingText:query location:alocation completion:^(NSArray* addresses, MDError *error){ //remoteAddresses is set dispatch_group_leave(taskGroup); }); 

This ensures that you do not leave the group until the termination block is called.


Alternatively, you can change searchForPlacesContainingText to use the dispatch_group_t parameter:

 - (void)searchForPlacesContainingText:(NSString *)searchText location:(CLLocation *)alocation group:(dispatch_group_t)group completion:(MDAddressManagerBlock)completionBlock { dispatch_group_enter(group); NSDictionary *parameters = [[NSMutableDictionary alloc] init]; [parameters setValue:searchText forKey:@"input"]; [[MDHTTPClient sharedHTTPClient] getPath:@"v1/remotePlaces.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id dict) { if ([MDHTTPClient isResponseValid:dict]) { completionBlock(returnArray, nil); } else { completionBlock(nil, nil); } dispatch_group_leave(group); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { EDLog(@"%@", error); completionBlock(nil, [MDError errorAFNetworking:error]); dispatch_group_leave(group); }]; } 

and change your call so that it does not execute dispatch_group_async , but simply taskGroup parameter:

 [[MDAddressManager instance] searchForPlacesContainingText:query location:alocation group:taskGroup completion:^(NSArray* addresses, MDError *error) { //remoteAddresses is set }); 
+13
source share

All Articles