NSMagedObjectContext, threads and NSFechedResultsController

Posted by tmpz on Stack Overflow See other posts from Stack Overflow or by tmpz
Published on 2010-05-25T23:33:21Z Indexed on 2010/05/27 11:21 UTC
Read the original article Hit count: 136

Filed under:
|

Dear iphone developers,

Core Data newbie speaking here.

In my application I have two NSManagedObjectContext that refer to that same NSPersistentStorageController.

One ManagedObjectContext (c1) is in the main thread --created when I create a NSFetchedResultsController -- and the second ManagedObjectContext (c2) created in a second thread, running in the background, detached from the main thread.

In the background thread I pull some data off a website and insert the entities created for the pulled data in the thread's ManagedObjectContext (c2).

In the meanwhile, the main thread sits doing nothing and displaying a UITableView whose data do be display should be provided by the NSFetchedResultsController.

When the background thread has finished pulling the data and inserting entities in c2, c2 saves, and the background thread notifies the main thread that the processing has finished before it exiting.

As a matter of fact, the entities that I have inserted in c2 are know by c1 because it can ask it about one particular entity with [c1 existingObjectWithID:ObjectID error:&error];

I would expect at this point, if I call on my tableview reloadData to see some rows showing up with the data I pulled from the web in the background thread thanks to the NSFetchedResults controller which should react to the modifications of its ManagedObjectContext (c1).

But nothing is happening! Only if I restart the application I see what I have previously pulled from the web!

Where am I doing things wrong?

Thank you in advance!

© Stack Overflow or respective owner

Related posts about iphone

Related posts about core-data