views:

219

answers:

1

I have a managed object context in a separate thread that is creating a few hundred managed objects, and when it saves, the did save notification is passed to the main thread and my other context (on the main thread) is updated:

In Thread

[ApplicationDelegate performSelectorOnMainThread:@selector(managedObjectContextDidSave:) 
           withObject:notification 
           waitUntilDone:NO];

The problem is that the merging is taking a very long time, sometimes 40-50 seconds, and this is locking up the main thread & UI. Is there any reason why it would take this long to update?

Edit

This appears to only happen if there is a fetched results controller that is currently displaying data that will be affected by the merging. Any ideas?

+2  A: 

I am assuming that your NSFetchedResultsController has a NSFetchedResultsControllerDelegate which is having to handle lots of updates as a result of the merge. These updates will result in lots of activity within the table, which might be causing the lag.

What you might have to do is create a wrapper method which when called on the main thread temporarily removes the delegate from the NSFetchedResultsController, then invokes managedObjectContextDidSave, then refreshed the table, then reinstates the NSFetchedResultsControllerDelegate.

lyonanderson
Thanks for your response. Yeah I've narrowed it down to the pesky delegate! I'd kind of assumed that all the FRC would do is perform another fetch and refresh the table view then it gets the notification, but it would appear that it does a lot more than that, hence why it takes so long! I've actually totally disconnected all my FRC's delegates as my implementation doesn't require anything more than a simple reload when it get's the notification. I just listen for the context's "did change" notification and fetch/reload. Thanks for your answer!
Michael Waterfall