Question

I have created entity with following methods

NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
Contact *contact = [Contact MR_createInContext:localContext];

And

Contact *contact = [Contact MR_createEntity];

Above methods are called inside a for loop and values are set. After that, I didn't executed any save method for above entity instance. However, after loop is completed, when I read from coredata,

NSArray *savedContacts = [Contact MR_findAll];
NSLog(@"Saved contacts : %d", [savedContacts count]);

It prints the saved entities count. So, are these entities autosaved? or are there any settings that is causing MagicalRecord to save each entities?

Any sort of information would be very essential. I am using MagicalRecord version 2.2. The issue has been opened here which points to ask question on SO with MagicalRecord tag.

Was it helpful?

Solution

I found the cause of this behavior.

FROM README DOCUMENT OF MAGICAL RECORD : CHANGES TO SAVING

MR_save has been temporarily restored to it's original state of running synchronously on the current thread, and saving to the persistent store. However, the MR_save method is marked as deprecated and will be removed in the next major release of MagicalRecord (version 3.0). You should use MR_saveToPersistentStoreAndWait if you want the same behaviour in future versions of the library.

So if you create a context from MR_contextForCurrentThread, then above rule will apply which causes each object you create to be saved in persistent store even if you don't call the save method. Similarly, the MR_createEntity method uses context from MR_contextForCurrentThread which was causing an object to save it. As stated above the MR_save is deprecated and will be removed in MR version 3.0 which will prevent it from autosaving in future even you create context via MR_contextForCurrentThread.

To create object with auto save off you can use local context from MR_context only as below :

NSManagedObjectContext *localContext = [NSManagedObjectContext MR_context];
Contact *contact = [Contact MR_createInContext:localContext];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top