I'm learning how to use MagicalRecord to manipulate CoreData, there are two method MR_saveToPersistentStoreWithCompletion and MR_saveToPersistentStoreAndWait. Look into comments, seems the difference is the first one save changes asynchronously, the later one save changes synchronously.
So that's the only difference between them? How they work? And what's the difference between asynchronously and synchronously save data?
I have looked into their source code, but cannot understanding totally as I am a beginner of it, so can somebody help to explain it?Thanks.
The asynchronous save is done inside the protection of performBlock
and the synchronous save is done inside the protection of performBlockAndWait
.
The asynchronous save will return immediately, and the save will happen on a background thread. If it's a main-queue context, the save will happen some time in the future, after the current runloop iteration is complete. When the save completes, the block of code passed in as a completion handler will be called to let you know that the save has completed.
Tha synchronous save will happen before the method invocation returns. Thus, when the method has returned, you know all the save work has been completed, or has encountered an error.
You should look at the Core Data documentation on concurrency for more detailed information.