Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between save: and processPendingChanges: in CoreData

I have an NSManagedObjectContext, i make a couple of changes to the model and then... to "commit" the transactions, what's the difference between doing:

[context save:&error];

and

[context processPendingChanges];

It seems they both do the same thing.

like image 921
ferostar Avatar asked Apr 19 '11 16:04

ferostar


People also ask

When managed object context saves its changes where does it push changes next?

When one of the managed object contexts is saved, its changes are pushed through the persistent store coordinator to the persistent store.

Is Coredata a Threadsafe?

Core Data is designed to work in a multithreaded environment. However, not every object under the Core Data framework is thread safe. To use Core Data in a multithreaded environment, ensure that: Managed object contexts are bound to the thread (queue) that they are associated with upon initialization.


1 Answers

In a nutshell, processPendingChanges changes the state of the current object graph. save will save the current object graph to disk.

Calling save will call processPendingChanges automatically.

If you think of a text file in a word processor, save is analogous to saving the document to disk.

processPendingChanges is analogous to telling the word processor to update it's internal state of the document after an edit, but without saving to disk. This usually triggers updates to the UI such as updating a displayed word or line count, doing any necessary formatting, etc...

In my experience, for the iPhone, you rarely need processPendingChanges. I believe it is mostly intended for Mac OS X and handling advanced or complicated undo management or updating UI bindings.

For the iPhone, this is usually done to trigger NSFetchedResultsControllers to update table views. Even then, this is somewhat rare. If you aren't sure just stick with save

For more info, go study the difference between NSManagedObjectContextDidSaveNotification and NSManagedObjectContextObjectsDidChangeNotification in the docs.

like image 71
amattn Avatar answered Oct 23 '22 11:10

amattn