Reputation: 233
I have a core data application based on the default xcode template, I have the main UI which is used for viewing the data and then I have a background thread which is downloading/inserting/editing data.
I figured when the application launches I can create two NSManagedObjectContext, one which the applications reads from and the other in the background thread will be written to, then when it has finished writing it will call performSelectorOnMainThread to sync the two NSManagedObjectContext objects.
I am fairly new to cocoa and wondered if anyone could confirm that this should work and does anyone know of an example or discussion about this as I cannot get the syncing to work correctly.
Upvotes: 2
Views: 1858
Reputation: 107754
You want to take a look at -[NSManagedObjectContext mergeChangesFromContextDidSaveNotification:]
. Register for change notifications from your worker thread's managed object context. In the notification callback, call your main thread's managed object context's mergeChangesFromContextDidSaveNotification
. Be sure you invoke this method on the main thread (the change notification will be posted on the work thread).
Upvotes: 3
Reputation: 36389
Are you manually creating the background thread? If you are, I would recommend following Cocoa's delegate pattern in NSURLConnection to do the actual downloading in the background and the processing of the data in the foreground.
If you feel you still need manually created threads, read the Multi-Threading in Core Data section of the Core Data Programming Guide for more insight.
Upvotes: 1