0

How can I manage calling an asynchronous block in a loop?

My code to be called n-times:

- (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];

                   }
               }];

}

I need to collect output data from these calls.

Any help will appreciated.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
Vanya
  • 4,973
  • 5
  • 32
  • 57
  • which problem are you having? – sergio Oct 10 '12 at 10:20
  • Since it is asynchronous call, it does not wait until the results are processed and then continue. The loop is done with just first parameters. – Vanya Oct 10 '12 at 10:23
  • and what would you do instead? sequencing the calls (e.g., wait for one to complete, and use it results)? – sergio Oct 10 '12 at 10:27
  • I am asking for the best approach regarding this situation. I have n records with latitude and longitude and those params are needed to be called with this function to get results for each record. I have done it recursively, but I do not like the solution. – Vanya Oct 10 '12 at 10:30

1 Answers1

1

Give your object a property, BOOL dunloadin.

Then, in the completion block, set self.dunloadin to YES.

Finally, your loop should look something like this:

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

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

However, you might want to consider designing your app differently so that kludges like this aren't required.

Amy Worrall
  • 16,250
  • 3
  • 42
  • 65