I have one view which fetches data from a private database with following code to fetch all private items:
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: false)],
animation: .default)
private var items: FetchedResults<Item>
and I have also a second view which fetches all PublicItems with the following code:
@Environment(\.managedObjectContext) var viewContext
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \PublicItem.timestamp, ascending: false)],
animation: .default)
private var publicItems: FetchedResults<PublicItem>
In my Persistence.swift is the following code:
import CoreData
import CloudKit
struct PersistenceController {
static let shared = PersistenceController()
static var preview: PersistenceController = {
let result = PersistenceController(inMemory: true)
let viewContext = result.container.viewContext
for _ in 0..<2 {
let newItem = Item(context: viewContext)
newItem.timestamp = Date()
newItem.name = "Test-Item"
}
do {
try viewContext.save()
} catch {
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
return result
}()
let container: NSPersistentCloudKitContainer
init(inMemory: Bool = false) {
container = NSPersistentCloudKitContainer(name: "ear2brain")
let privateStoreDescription = NSPersistentStoreDescription(url: FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("private.sqlite"))
privateStoreDescription.configuration = "private"
privateStoreDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "iCloud.ear2brain")
privateStoreDescription.cloudKitContainerOptions?.databaseScope = .private
let publicStoreDescription = NSPersistentStoreDescription(url: FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("public.sqlite"))
publicStoreDescription.configuration = "public"
publicStoreDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "iCloud.ear2brain")
publicStoreDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
publicStoreDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
publicStoreDescription.cloudKitContainerOptions?.databaseScope = .public
container.persistentStoreDescriptions = [privateStoreDescription, publicStoreDescription]
if inMemory {
container.persistentStoreDescriptions.first?.url = URL(fileURLWithPath: "/dev/null")
container.persistentStoreDescriptions.last?.url = URL(fileURLWithPath: "/dev/null")
}
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
do {
try container.viewContext.setQueryGenerationFrom(.current)
} catch {
fatalError("Failed to pin viewContext to the current generation:\(error)")
}
}
}
Only the data in the private store gets synced between the users devices, but the data from the publicitems doesn't get synced at all.
Here are my configurations:
PublicItem and Item have the same attributes, but PublicItem is from public configuration and Item from private configuration. All of configurations ("public","default" and "private") are marked as used with CloudKit. Does anyone know what I'm not going through?