Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the difference between NSManagedObjectContext reset and rollback?

Tags:

The documentation says:

- (void)reset 

Returns the receiver to its base state.

Discussion

All the receiver's managed objects are “forgotten.” If you use this method, you should ensure that you also discard references to any managed objects fetched using the receiver, since they will be invalid afterwards.


- (void)rollback 

Removes everything from the undo stack, discards all insertions and deletions, and restores updated objects to their last committed values.

Discussion

This method does not refetch data from the persistent store or stores.

It seems that after I make some change to my context, calling these two method will do exactly the same thing: discard changes and restore updated objects to their last committed values. So what does -reset actually do?

like image 644
caiguo Avatar asked Nov 30 '11 01:11

caiguo


People also ask

What is the purpose of NSManagedObjectContext?

A managed object context is an instance of NSManagedObjectContext . Its primary responsibility is to manage a collection of managed objects. These managed objects represent an internally consistent view of one or more persistent stores.

What is NSManagedObjectContext in Core Data?

An object space to manipulate and track changes to managed objects.


2 Answers

The key part is in the quote

All the receiver's managed objects are “forgotten”.

- (void)reset; will give you a clean NSManagedObjectContext with no objects in it and as the docs state any NSManagedObject's you have around should be discarded as they are no longer valid.

- (void)rollback will just restore the NSManagedObject's to their persisted values

like image 108
Paul.s Avatar answered Oct 01 '22 16:10

Paul.s


-reset is different than -rollback in that it invalidates any NSManagedObjects that have been fetched from the context. Attempting to use those objects can be expected to throw an exception. However -rollback simply discards unsaved changes.

like image 37
Lily Ballard Avatar answered Oct 01 '22 17:10

Lily Ballard