Devfly
Devfly

Reputation: 2495

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.

Upvotes: 4

Views: 2720

Answers (1)

Lily Ballard
Lily Ballard

Reputation: 185811

-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.

Upvotes: 8

Related Questions