Clearing a context in Core Data: reset vs deleting registered objects?

AppsDev picture AppsDev · Aug 25, 2015 · Viewed 7.6k times · Source

I was looking for posts regarding to this, but I don't fully understand... What is the difference between:

[context reset];

and:

for (NSManagedObjectID *objId in objectIds) {
  [context deleteObject:[context objectWithID:objId]];
}

Or are they equivalent?

Thanks

Answer

Tom Harrington picture Tom Harrington · Aug 25, 2015

Using reset puts the managed object context back to the state it was in when you first created it-- before you had performed any fetches, created any new objects, etc. If you have any managed objects in memory that were fetched from this context, they're now unusable. Using reset does not affect the persistent store file. All instances still exist afterward, they're just not in memory. They can be fetched again.

Using deleteObject removes the object from the persistent store. It does not exist any more. It can't be fetched anymore because it doesn't exist.