2012-10-10 5 views
0

루프에서 비동기 블록 호출을 어떻게 관리 할 수 ​​있습니까? 루프의 비동기 블록

내 코드

는 N-번 호출 될 :

- (void) convertToCountries:(NSString*)time longitude:(NSString*)lon latitude:(NSString*)lat { 


CLLocation *myLocation = [[CLLocation alloc] initWithLatitude:[lat doubleValue] longitude:[lon doubleValue]]; 


[geocoder reverseGeocodeLocation:myLocation 
       completionHandler:^(NSArray *placemarks, NSError *error) { 
        NSLog(@"reverseGeocodeLocation:completionHandler: Completion Handler called!"); 

        if (error){ 
         NSLog(@"Geocode failed with error: %@", error); 
         return; 
        } 

        if(placemarks && placemarks.count > 0) 
        { 
         //do something 
         CLPlacemark *topResult = [placemarks objectAtIndex:0]; 
         NSString *addressTxt = [NSString stringWithFormat:@"%@, %@ %@,%@ %@", [topResult country], 
               [topResult subThoroughfare],[topResult thoroughfare], 
               [topResult locality], [topResult administrativeArea]]; 
         NSLog(@"%@",addressTxt); 

         NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
         [formatter setDateFormat:@"yyyy-MM-dd"]; 

         //Optionally for time zone converstions 
         [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]]; 


         [DataOperations saveRecord:[topResult country] time:time]; 

        } 
       }]; 

} 

내가 이러한 호출의 출력 데이터를 수집해야합니다.

도움을 주시면 감사하겠습니다.

+0

어떤 문제가 있습니까? – sergio

+0

비동기 호출이므로 결과가 처리 될 때까지 기다리지 않고 계속 진행합니다. 루프는 첫 번째 매개 변수만으로 완료됩니다. – Vanya

+0

그리고 대신 무엇을 하시겠습니까? 통화 순서를 지정합니다 (예 : 완료 될 때까지 기다린 후 결과 사용). – sergio

답변

1

물건에 재산을 부여하십시오, BOOL dunloadin.

그런 다음 완료 블록에서 self.dunloadinYES으로 설정하십시오.

마지막으로, 루프는 다음과 같이 보일 것이다 :

for (int i=0; i<10; i++) 
{ 
    self.dunloadin = NO; 
    [self convertToCountries:…]; 

    while (!self.dunloadin) 
    { 
     [[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; 
    } 
} 

그러나이 같은 잡든지이 필요하지 않도록 다르게 응용 프로그램을 설계하는 것을 고려할 수 있습니다.

+0

잘 작동합니다. – Vanya