Cocoa Master Data - background theme

I have a main data app based on the default xcode template, I have a main UI that is used to view the data and then I have a background thread that loads / inserts / edits the data.

I realized that when the application starts, I can create two NSManagedObjectContext, from which the applications will write, and the other on the background thread, and then when it finishes writing, it will call performSelectorOnMainThread function to synchronize the two NSManagedObjectContext objects.

I am new to cocoa and am wondering if anyone can confirm that this should work and does anyone know of an example or discussion about this as I cannot get the sync to work correctly.

+2


source to share


2 answers


You want to take a look at -[NSManagedObjectContext mergeChangesFromContextDidSaveNotification:]

. Register for change notifications from the context of the workflow managed object. In the notification callback, call the context of the stream managed object mergeChangesFromContextDidSaveNotification

. Make sure you call this method on the main thread (the change notification will be posted on the worker thread).



+3


source


Are you manually creating a background thread? If so, I recommend following the example of the Cocoa delegate pattern in NSURLConnection to do the actual background loading and foreground data processing.



If you think you still need manually created threads, read the Multi-Threading in Core Data section of the Master Data Programming Guide for more understanding.

+1


source







All Articles