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]); }]; }
asynchronous ios queue objective-c-blocks grand-central-dispatch
Ilker Baltaci
source share