Question

I am working on a application where the app fetches images from facebook. I am using core data to save the image. I use dispatch_asyc function to save the image in the Core Data. I use

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)

to save the image so that my main thread doesn't block.

But some times I observed that app freezes and doesn't respond. I use the same way of saving image to core data at different places in the app, like if user goes to signed user profile, then app saves his album photos in the same way. Also if user goes to full screen of any image, it starts fetching of comments, likes and saves in the same way.

If I use a private queue and use the same for saving then app doesn't freeze.

Since I am using the global queue at many places in the app, is this the reason for freeze of the app?

Thanks.

Was it helpful?

Solution 2

This issue has been fixed. The issue was accessing of main context by multiple threads, because of this reason the app was going into freeze state. Now this issue has been fixed.

OTHER TIPS

You should really read the Core Data Programming guide section about concurrency here. It sounds like you're accessing a managedObjectContext outside of the thread that created it which is illegal. You would probably be better off using a child context with performBlock to perform this task. Something like this:

NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[context setParentContext:parentContext];
[context performBlock:^{
   //Save image
}];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top