0
votes

In one of my apps I would like to enable sync via CloudKit. The app itself stores the data using CoreData. I cannot use CoreData+iCloud because I need to be able to track changes which were made which is not possible with the above mentioned setup.

The obvious challenge which I have relates to the fact that CloudKit operations will be happening on the background thread and so I need to support concurrency with my CoreData stack.

Lets assume that I have some CoreData entity which I want to sync with CloudKit. To do background sync I need to create a separate managed object context. When this context fetches changes made to my entity on other devices I want to save these changes.

However, the problem is that during the execution of fetch user could have changed some attributes of a given entity and I am now stuck with this entity having some important information on both contexts. I cannot just choose between which one to save - I need to merge the changes.

Unfortunately, I do not understand which approach I need to take to be able to manually sync these changes. I have already checked the possible techniques (parent-child, multiple contexts, multiple store coordinators) but I still don't understand which approach I need to choose.

Can someone help me to resolve this confusion?

The most ideal solution for me would be such that I would get a conflict whenever sync context tries to merge changes to the main context which also had some changes. In this case I could easily decide which attributes should be updated, but I really don't know how this can be done...

1
If you have time I suggest you read this awesome book: objc.io/books/core-data It finally gave me an understanding how the beast works. It also has a solution for your exact problem as a case-study on conflict handling between different contexts. Comes with access to code too. - juhan_h
Thank you for a suggestion! I will definitely consider reading the full book. Is there any chance you can at least provide a hint as to what approach is best in my case? - Andriy Gordiychuk
@AndriyGordiychuk hey, did you solve the issue? I’m having almost same problem, sync with cloud gives me a bunch of different conflicts that I don’t know how to solve. Will be so helpful if you will share your solution. Thanks in advance - Woof

1 Answers

1
votes

In Apple WWDC 2014 they did a session on just this in effect. Here is the transcript, you can find the video easily too; assuming your Apple Developer.

http://asciiwwdc.com/2014/sessions/231

In in short an Apple engineer goes over a very simple example of using change tokens; an industry standard solution to resolving problem of sorting out data change conflicts.

The Ray Wenderlich web site also has some excellent VIDEOs on cloud kit and core data, it is a subscription service, although well worth the money at just 20$ a month, and no I have no affiliation with him; I'm just a subscriber. Google him and your find his website.