I have an app in production using Core Data + CloudKit, synced using the built in NSPersistentCloudKitContainer.
It works perfectly well most of the time, but sometimes it simply won't sync with no errors or anything.
Some users report that it works perfectly from iPad to iPhone, but not from iPhone to iPad. Some users even report randomly losing data. I've checked thoroughly for bugs but I really can't figure it out. Has anyone had issues with this? Are there any solutions or at least things to try? Here's the data container code:
lazy var container: NSPersistentContainer = {
let container = NSPersistentCloudKitContainer(name: "<AppName>")
let directory = FileManager.default.urls(
for: .applicationSupportDirectory, in: .userDomainMask).first!
//Local data
let localStoreDescription = NSPersistentStoreDescription(url: directory.appendingPathComponent("Local.sqlite"))
localStoreDescription.configuration = "Local"
//Cloud synced data
let cloudStoreDescription = NSPersistentStoreDescription(
url: directory.appendingPathComponent("Cloud.sqlite"))
cloudStoreDescription.configuration = "Cloud"
cloudStoreDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(
containerIdentifier: "<ContainerIdentifier>")
container.persistentStoreDescriptions = [
cloudStoreDescription,
localStoreDescription
]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
container.viewContext.automaticallyMergesChangesFromParent = true
container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
return container
}()