Thursday, April 18, 2013

The Concurrent Core Data Stack

Florian Kugler:

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.

The key insight here is that managed object contexts in the same hierarchy can have different concurrency types. However it seems to me that this could potentially push a lot of work onto the main thread as fetches from that context would have to reconcile the differences between the disk and memory.

Update (2013-04-29): Florian Kugler:

Core Data stack #3 with its independent managed object contexts blew the other two options out of the water. It literally crushed them. I didn’t expect the nested setup #2 to be so much inferior.

Update (2013-05-13): Florian Kugler:

In this article I will take a look behind the scenes of how nested managed object contexts operate and explain why the performance of saving a child context often is much worse than manually merging changes into a context. Understanding how each approach works will make clear, that nested contexts are certainly not a replacement for “manual” merging.

2 Comments RSS · Twitter

> fetches from that context would have to reconcile the differences between the disk and memory

I am not sure what you mean. It seems to me that the UI context will only need to fetch from the parent context stuff that it does not have in memory. Things that have not been pushed to the master context (and are not yet on disk) are by definition still in memory. So basically, I am not sure what you mean by 'reconcile'.

@charles That’s exactly what I’m talking about. The UI context will have to do a fetch from disk and also walk the objects it has in memory. From what I’ve seen, this second part can be really slow. By “reconcile” I mean that neither location alone has the full, up-to-date data.

Leave a Comment