How to Save Using MagicalRecord
I spent some time to research on the correct/recommended way to save using MagicalRecord, and felt a need to write this post.
It is relevant as of March 2013, using version 2.2 of MagicalRecord.
Tutorials out there
I wrote this because even tutorials eg from Ray Wenderlich (2014 Feb) didn’t use the recommended methods.
Or tutorials eg from Yannick Loriot (2012 Mar) are outdated.
And unfortunately, the ‘official document’ on saving explains more on the recent changes to saving. That is least helpful.
Though if you dig further into threading, you will find a better and safer way to save.
And that is using
What the author says
On StackOverflow, he said:
On his blog, he explained why.
Finally, here is the code to update an entity.
Poo is my
NSManagedObject. Replace it with your own model.
1 2 3 4 5 6 7
Or if you want to delete the entity, use
[localPoo deleteEntity] in the block.
Note that the block will be run in a background thread, not the main thread.
So if you want to do some UI updating, you should use
1 2 3 4 5
Alternatively, you can use
1 2 3
Hopes this helps you!