Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between managed object context save and refreshObject:mergeChanges:

Hello what's the difference between

  [self.context refreshObject:site mergeChanges:YES];

and

  [self.context save:nil];

Sometimes I use them both, sometimes I use only save. It works in both cases.

like image 591
Devfly Avatar asked Feb 18 '23 03:02

Devfly


1 Answers

-save: saves the changes you've made to any managed object in the context. This means they get flushed to the persistent store coordinator, which then writes them to the persistent store, which writes them to disk (assuming a disk-backed store).

On the other hand, -refreshObject:mergeChanges: does something quite different. It reads the current state of the object from the persistent store coordinator (which reads from the persistent store, and so on). Passing YES for mergeChanges means to keep any local modifications to the object intact and only update the fields that weren't changed. This is pretty much the opposite of -save:.

As a trivial thought experiment, if you run -save: and then terminate your app, on next launch your modified data will still be available. If you run -refreshObject:mergeChanges: and then terminate your app, any of your local changes will be lost.

like image 149
Lily Ballard Avatar answered Mar 23 '23 01:03

Lily Ballard