Writer-Main-Worker Core Data Stack

xCode 7.3 demo

stack2

prvt-main-bckgnd

We set up our stack just like the diagram. Note that in _mocDidSaveNotification method:

if your savedContext.parentContext is the main context, it will pause the main thread a little bit in order to do that save. Hence, you may want to use a wait cursor, or something to signify that the main UI will be unresponsive.

Topmost, writer MOC (Master Context)

First, we have the top most MOC where parent is nil. It is connected to a PSC.

Main MOC (Main Context)

Whenever a save is called on a context, we go to _mocDidSaveNotification.
Save will be called on the main queue, which propagates the changes up to the writer private Queue.
Our purpose is that in _mocDidSaveNotification, we want to have the writer private Queue save,
So that it propagates its changes to the PSC.

When a worker MOC saves, it calls _mocDidSaveNotification. _mocDidSaveNotification method will then make the parent save.
The parent would be the Main Context. When the Main Context saves, it will call _mocDidSaveNotification again, which will make
Main Context’s parent (Writer Context/Master) save.

Due to Writer Context’s save, _mocDidSaveNotification will return because Writer Context’s parent context is nil. The save at Writer context’s
level will write the changes into PSC.

Spawning the Worker MOCs

Say we want to create a record. We Create a worker MOC. The important thing here is to connect that worker MOC
as a child to the main context. Then throw a task block onto the worker MOC and let it run.

Since the Writer (master) context is running on a background thread, all the heavy lifting of persisting data to disk and reading data from disk is done without blocking the main thread.

There is also a very clean flow of data from the worker contexts through the main context to the master context, which finally persists all changes. Since in this setup you never touch the master context, i.e. you never directly make changes to it, all changes flow through the main context. Therefore you will always have the latest data available on the main thread. No need for listening to change notifications and merging changes manually.