2017-06-15 25 views
0

在我的代码我有多个位置,我必须检查所有的位置是否正确(检查与谷歌API)如果位置是正确的我必须得到该位置的坐标。如何等待for循环(Xcode,Objective C,iOS)中的callBack方法(或)响应?

我想在for循环中编写代码是否有任何方法来等待for循环中的响应。

我在下面粘贴我的代码。

在此先感谢。

for (int locationsCount=0;locationsCount<results.count;locationsCount++) 
    { 
     NSString *googlelocations = [[results objectAtIndex:locationsCount]objectForKey:@"description"]; 

     if ([locationAddress isEqualToString:googlelocations]) 
     { 
      [[LocationManager share] fetchLatLngForPlacename:googlelocations placeId:[[results objectAtIndex:locationsCount] objectForKey:@"place_id"] completion:^(double lat, double lng, NSError *error) 
      { 
       [SVProgressHUD dismiss]; 

       if (error) { 

       }else { 

        CLLocation *locationCoordinates = [[CLLocation alloc]initWithLatitude:lat longitude:lng]; 

        NSMutableArray *globalArray = [[LocationManager share]getManualInterLocationArray]; 
        NSMutableDictionary *dict = [[globalArray objectAtIndex:selectedTextField.tag] mutableCopy]; 

        [dict setObject:locationCoordinates forKey:@"location_coordinates"]; 

        [dict setObject:googlelocations forKey:@"location_Address"]; 

        [dict setObject:[NSNumber numberWithBool:true] forKey:@"manualEntry_Flag"]; 

        [globalArray replaceObjectAtIndex:selectedTextField.tag withObject:dict]; 

        [[LocationManager share]saveManualInterLocationArray:globalArray]; 

       } 

      }]; 
     } 
    } 

回答

0

我对此要求使用递归方法它现在工作正常。递归方法是实现这一要求的最佳和简单的方法。

-(void)addressValidation:(int)locationCount andCompletion:(void(^)(BOOL isSuccess))callBack{ 
if (manualarraycount >= globalArray.count) 
{ 
    callBack(true); 
    return; 
} 

[[LocationManager share] fetchOfflineAutoCompleteSuggestionForKey:locationAddress LocationCoordinates:location Radius:duration completion:^(NSArray *results, NSError *error){ 
    // --------- do what you want ------------ 
    [self addressValidation:manualarraycount+1 andCompletion:callBack]; 
}]; 

}

0

尝试使用递归。创建一个函数

-(void)setLocation:(NSUInteger)locationCount andCompletion:(void (^))completionBlock{ 
    if (locationsCount>=results.count) { 
     if (completion) { 
       completion(); 
     } 
     return; 
    } 
    NSString *googlelocations = [[results objectAtIndex:locationsCount]objectForKey:@"description"]; 

    if ([locationAddress isEqualToString:googlelocations]) 
    { 
     [[LocationManager share] fetchLatLngForPlacename:googlelocations placeId:[[results objectAtIndex:locationsCount] objectForKey:@"place_id"] completion:^(double lat, double lng, NSError *error) 
     { 
      [SVProgressHUD dismiss]; 

      if (error) { 

      }else { 

       CLLocation *locationCoordinates = [[CLLocation alloc]initWithLatitude:lat longitude:lng]; 

       NSMutableArray *globalArray = [[LocationManager share]getManualInterLocationArray]; 
       NSMutableDictionary *dict = [[globalArray objectAtIndex:selectedTextField.tag] mutableCopy]; 

       [dict setObject:locationCoordinates forKey:@"location_coordinates"]; 

       [dict setObject:googlelocations forKey:@"location_Address"]; 

       [dict setObject:[NSNumber numberWithBool:true] forKey:@"manualEntry_Flag"]; 

       [globalArray replaceObjectAtIndex:selectedTextField.tag withObject:dict]; 

       [[LocationManager share]saveManualInterLocationArray:globalArray]; 

      } 

     }]; 
    } 
} 

在你完成块调用函数本身与增量次数:

[self setLocation:locationCount++ andCompletion:nil]; 

,并开始你的重复调用,你需要从0

[self setLocation:0 andCompletion:^{ 
// handle completion 
}]; 
+0

我想要的方式进行回调。可能吗? –

+0

是的! A编辑了答案 – iamirzhan

+0

上述方法被回调函数调用,所以完成该过程后我们必须发送确认才能继续下一个过程。如果(appDelegate()。internetSatus == false){ return;}(void)(void } addressArrayCount = 0; (自动地址验证:地址数组和计数完成:^(BOOL isSuccess) { if(isSuccess){callBack(true); NSLog(@“completed”); } }]; } –

0

启动功能可以使用调度组,如在此伪代码中:

dispatch_group_t loadDetailsGroup=dispatch_group_create(); 

for(id thing in thingsToDo) 
{ 
    dispatch_group_enter(loadDetailsGroup); 

    // call method with completion callback, and in the callback run 
    dispatch_group_leave(loadDetailsGroup); 
} 

// Now outside the loop wait until everything is done. NOTE: this will block! 
dispatch_group_wait(loadDetailsGroup, DISPATCH_TIME_FOREVER); 

如果您在主线程上运行此操作,则不应该阻止它,以便UI保持响应。所以,你可以在后台做等待部分,完成后则可能做一些在主线程:

// to background 
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{ 
    // wait in background 
    dispatch_group_wait(loadDetailsGroup, DISPATCH_TIME_FOREVER); 

    // back to main (not needed if what you need to do may happen in background) 
    dispatch_async(dispatch_get_main_queue(),^{ 

     // do stuff here that affects the UI 
    }); 
}); 

编辑:库尔特雷维斯指出,如果要异步等待,有所回调, dispatch_group_notify()更适合这一点。所以整个上面的代码可以被压缩到:

dispatch_group_t loadDetailsGroup=dispatch_group_create(); 

for(id thing in thingsToDo) 
{ 
    dispatch_group_enter(loadDetailsGroup); 

    // call method with completion callback, and in the callback run 
    dispatch_group_leave(loadDetailsGroup); 
} 

// Now outside the loop wait until everything is done. NOTE: this will 
// not block execution, the provided block will be called 
// asynchronously at a later point. 
dispatch_group_notify(loadDetailsGroup,dispatch_get_main_queue(),^{ 

    // Callback 
}); 
+1

在你的第二个代码块中,执行'dispatch_group_notify(loadDetailsGroup,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^ {...});是不是更直接?这样你可以避免让队伍坐在队列中等待。 –

+0

好点Kurt,谢谢!我修改了我的例子。 –