Reputation: 5836
I'm doing a project using core data, and as far as I know anything done in the core data thread has to remain in said thread.
I do a call to an API to download some news items and then load them into the database:
[self.database.managedObjectContext performBlock:^{
for (NSDictionary *itemInfo in result) {
NSLog(@"%@", itemInfo);
[Item createItemWithInfo:itemInfo inManagedObjectContext:self.database.managedObjectContext];
}
[self.database.managedObjectContext save:nil];
}];
In my create method, besides setting up all the data in the objects I have an additional call to get an image related to the news item in question:
+ (Item *)createItemWithInfo:(NSDictionary *)info inManagedObjectContext:(NSManagedObjectContext *)context {
Item *item;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Item"];
request.predicate = [NSPredicate predicateWithFormat:@"itemId = %@", [info valueForKeyPath:@"News.id_contennoticias"]];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"itemId" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSError *error = nil;
NSArray *matches = [context executeFetchRequest:request error:&error];
if (!matches || ([matches count] > 1)) {
// handle error
} else if ([matches count] == 0) {
item = [NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext:context];
item.itemId = [NSNumber numberWithInteger:[[info valueForKeyPath:@"News.id_contennoticias"] integerValue] ];
item.title = [info valueForKeyPath:@"News.titulo_contennoticias"];
item.summary = [info valueForKeyPath:@"News.sumario_contennoticias"];
item.content = [info valueForKeyPath:@"News.texto_contennoticias"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.dateFormat = @"yyyy-MM-dd hh:mm:ss";
NSDate *creationDate = [dateFormat dateFromString:[info valueForKeyPath:@"News.fechacre_contennoticias"]];
item.creationDate = creationDate;
dispatch_queue_t imageDownloadQueue = dispatch_queue_create("image downloader", NULL);
dispatch_async(imageDownloadQueue, ^{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/files/imagenprincipal/%@", BASE_PATH, [info valueForKeyPath:@"News.imgprincipal"]]];
NSData *imageData = [NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_current_queue(), ^{
item.image = imageData;
});
});
} else {
item = [matches lastObject];
}
return item;
}
On this part:
dispatch_async(dispatch_get_current_queue(), ^{
item.image = imageData;
});
I'm getting the error, my app just dies there. Also it says that dispatch_get_current_queue()
is deprecated in iOS 6.
Upvotes: 0
Views: 2573
Reputation: 7656
Since dispatch_get_current_queue()
is called from within a block on the imageDownloadQueue
, why aren't you using imageDownloadQueue
directly? If you want to run it on the moc queue, then make sure not to use dispatch_get_current_queue()
.
In general be careful with using dispatch_get_current_queue()
. Quoting from the Dispatch Queues page in the Apple Concurrency Programming guide:
Use the dispatch_get_current_queue function for debugging purposes or to test the identity of the current queue.
Upvotes: 2