1

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: enter image description here 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?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
someone
  • 65
  • 6
  • 1
    I recommend watching the WWDC video on it, the developer has a big rant and seems like they didn't want to implement public syncing at all because its so inefficient – malhal Jun 27 '23 at 15:19
  • @malhal : Why do you say so? Is it inefficient because it takes a long time to sync and refreshes just with each app launch and every 30 minutes or because it stores all data on each device too? – someone Jun 27 '23 at 16:30
  • 1
    I think it was because it downloads the entire database every time, it can't sync just changes like the private db. Check the video for more info. – malhal Jun 27 '23 at 17:51
  • @malhal : So should I just wait longer? If I query for records of PublicItem in the CloudKit Dashboard I don't get any results, so probably there is an upload error – someone Jun 27 '23 at 19:46

0 Answers0