Skip to content

Instantly share code, notes, and snippets.

@g-1
Created April 8, 2014 15:40
Show Gist options
  • Save g-1/10144897 to your computer and use it in GitHub Desktop.
Save g-1/10144897 to your computer and use it in GitHub Desktop.
GCDでWaitForMultipleObjectsを代替する方法 ref: http://qiita.com/g-1/items/dde3f3c34c50c6d6f055
dispatch_group_t group = dispatch_group_create();
[self.arrayLocalSearch removeAllObjects];
NSError* error = nil;
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription* entity = [NSEntityDescription entityForName:@"Location"
inManagedObjectContext:[MapData sharedManager].managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:40];
NSArray* locations = [[MapData sharedManager].managedObjectContext executeFetchRequest:fetchRequest error:&error];
if(error){
LOG(@"Unresolved error %@ %@",error,[error userInfo]);
}
for(Location* location in locations){
dispatch_group_enter(group);
if (!location.geocoding) {
[self searchShopLocation:location
block:^{
dispatch_group_leave(group);
}];
}else{
CustomMapItem* customItem = [[CustomMapItem alloc] init];
customItem.location = location;
self.mapAnnotations[location.name] = customItem;
dispatch_group_leave(group);
}
}
//全ての非同期処理が完了したら、メインスレッドで実行される
dispatch_group_notify(group,
dispatch_get_main_queue(),
^{
//NSLog(@"done");
[self.mapView removeAnnotations:self.mapView.annotations]; // remove any annotations that exist
NSMutableArray* array = [NSMutableArray arrayWithCapacity:self.mapAnnotations.count];
for (id key in [self.mapAnnotations keyEnumerator]) {
[array addObject:self.mapAnnotations[key]];
}
[self.mapView addAnnotations:array];
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment