Search code examples
ioscore-dataconcurrencynsmanagedobjectnsmanagedobjectcontext

How to manage Core Data context when there could be updates of data in background?


I've been looking for posts related to this scenario, but I don't have a clear idea of how should I manage it: I have a context that could have several (maybe quite a lot) managed objects that the application may be using to perform operations, or even the user could be editing them, and meanwhile I can receive updates of the information in such objects from a service. Updating those objects while the user is editing them or the app is using them to perform operations and calculations could be a problem, as well as saving the context for the update received. I need somehow to "block" the objects being used when I concurrently need to save the updates I receive.

I hope I'm explaining the scenario clearly... how could/should I manage it?


Solution

  • What you want to do is handle server updates on a child context as defined in the latest Core Data Programming Guide. Then set the merge policy on your main queue context to whatever makes sense for your business logic.

    From there you let Core Data handle the merges. That is one of the primary features of Core Data.