0

I have several different apps that I'd like to share a personal core data store with on the same device and across devices. I've been experimenting with NSPersistentCloudKitContainer, which seems to work, but it can take a while to sync and it's not clear when it has. I have also experimented with using app groups where the SQLite file is placed in a group folder so that changes are immediately available to other apps on the device. My question is whether it is better to do one, or the other or both. Will two apps open on the same device accessing the shared SQLite cause conflict issues? Do I even need to sync data with cloudKit if I'm already syncing with a shared group? Using NSPersistentCloudKitContainer alone means that each app will be syncing its own copy of the data locally, which doesn't seem very efficient and again I don't know if it's synched when I open the other app up. But I have seen conflicts when I have both apps open at the same time. Is there a foolproof way to prevent conflicts when one app goes into the background?

Toby Evetts
  • 123
  • 1
  • 9

1 Answers1

1

App groups and CloudKit are orthogonal to each other. They solve different problems and you can use one or the other or both depending on what you want to do. App groups are good for sharing data on one device, and CloudKit works across multiple devices. You can use both if you want both of those things to happen. It's not one or the other, it's either one or both depending on the app.

The only foolproof way to avoid conflicts would be to never edit the same data in more than one place. Since that probably won't happen with either of these, it's better to look at how to resolve conflicts when they arise. Core Data has several built in merge policies to resolve conflicting changes, and you can write your own if you want something different.

With CloudKit your app will be notified when new changes are available. Merge changes then, and let the merge policy deal with conflicts. With app groups there's no notification of external changes, so the best approach is to check for new data whenever the app comes to the foreground (in case it was edited somewhere else).

If you're seeing conflicts you don't know how to resolve, post a separate question with details of the problem and someone can probably help with it.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • I’ll take a look at conflicts. I was actually getting occasional exceptions that crashed the apps when I switched between them, and the log suggested something to do with coredata. But that was running from Xcode so perhaps that was the problem. What benefit would there be to having app groups and nspersistentcloudkit running at the same time? – Toby Evetts Jun 03 '22 at 14:33
  • The benefit would be that if you need both features, you get both features. Like I said, they do different things. If you need both of the things they do, use both. If you don't need both things, don't use both. – Tom Harrington Jun 03 '22 at 20:19
  • Ok, I think what I missed is that appgeoups are not shared across devices by default. I thought that because it looked like a CloudKit container it also acted like one. So doing both is the key. – Toby Evetts Jun 04 '22 at 22:07