My app is composed from 2 components: iOS app (public facing) + macOS app (where I will feed in public info).
Both will share the same codebase for Services and Model.
The app will have public content (that will be stored in the public db - and only I will have the option to modify) and private content that each user will be able to create on their private database.
The model is basically simple:
An entity A with primitive fields AF1, AF2, AF3
An entity B with primitive fields BF1, BF2
A OneToMany relation between A and B (A can have many B).
The problematic flow:
I save info to the public db via Mac app (check it via CK dashboard)
I validate the data on the iOS app via a real device (bring app the background, bring app to foreground, wait a couple of seconds and the data is there)
I do various tests like: Adding a new B to A -> save -> check or updating a B -> save -> check.
Most of the time everything works like a charm (obvious the data comes with a delay, but it comes).
The problem is that sometimes when I update via macAPP, I get partial data on the iOS app (e.g. the A table is always updated (by checking the primate fields) and the B relations will never get updated.
Notes:the CK dashboard if up to date each time I trigger a save from Mac app
on the iOS app, when the records don't reach the device, I manually check the sqlite DB and I can confirm that it's inconsistent.
I blame the coredatastack, but I really didn't get much info for public database setup.
My CoreDataStack is shared on both apps and looks like this:
		
		let cloudStoreLocation = storeDirectory.appendingPathComponent("cloud.sqlite")
		let cloudStoreDescription = NSPersistentStoreDescription(url: cloudStoreLocation)
		cloudStoreDescription.configuration = privateCloudStoreName
		cloudStoreDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "iCloud.ABC")
		cloudStoreDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
		cloudStoreDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
		let publicCloudStoreLocation = storeDirectory.appendingPathComponent("cloud-public.sqlite")
		let publicCloudStoreDescription = NSPersistentStoreDescription(url: publicCloudStoreLocation)
		publicCloudStoreDescription.configuration = publicCloudStoreName
		var cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "iCloud.ABC")
		cloudKitContainerOptions.databaseScope = .public
		publicCloudStoreDescription.cloudKitContainerOptions = cloudKitContainerOptions
		
		if isiOSApp { // only iOS will load a private store
				container.persistentStoreDescriptions = [cloudStoreDescription, publicCloudStoreDescription]
		} else {
				container.persistentStoreDescriptions = [publicCloudStoreDescription]
		}
		container.loadPersistentStores(completionHandler: { (_, error) in
				guard let error = error as NSError? else { return }
				fatalError("###\(#function): Failed to load persistent stores:\(error)")
		})
		container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
		container.viewContext.transactionAuthor = "appauthname"
		container.viewContext.automaticallyMergesChangesFromParent = true
		do {
				try container.viewContext.setQueryGenerationFrom(.current)
		} catch {
				fatalError("###\(#function): Failed to pin viewContext to the current generation:\(error)")
		}
		NotificationCenter.default.addObserver(
				self, selector: #selector(type(of: self).storeRemoteChange(_:)),
				name: .NSPersistentStoreRemoteChange, object: nil)
4
0
2.1k