ios – CloudKit sync in extension with AppGroups


That is my persistence container:

    lazy var persistentContainer: NSPersistentCloudKitContainer = {
    let container = NSPersistentCloudKitContainer(identify: "ListModel")
    
    guard let privateStoreDescription = container.persistentStoreDescriptions.first else {
        fatalError("Unable to get persistentStoreDescription")
    }
    let storesURL = privateStoreDescription.url?.deletingLastPathComponent()
    privateStoreDescription.url = storesURL?.appendingPathComponent("personal.sqlite")
    let sharedStoreURL = storesURL?.appendingPathComponent("shared.sqlite")
    guard let sharedStoreDescription = privateStoreDescription.copy() as? NSPersistentStoreDescription else {
        fatalError("Copying the personal retailer description returned an surprising worth.")
    }
    sharedStoreDescription.url = sharedStoreURL
    
    guard let containerIdentifier = privateStoreDescription.cloudKitContainerOptions?.containerIdentifier else {
        fatalError("Unable to get containerIdentifier")
    }
    let sharedStoreOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: containerIdentifier)
    sharedStoreOptions.databaseScope = .shared
    sharedStoreDescription.cloudKitContainerOptions = sharedStoreOptions
    container.persistentStoreDescriptions.append(sharedStoreDescription)
    
    container.loadPersistentStores { loadedStoreDescription, error in
        if let error = error as NSError? {
            fatalError("Didn't load persistent shops: (error)")
        } else if let cloudKitContainerOptions = loadedStoreDescription.cloudKitContainerOptions {
            guard let loadedStoreDescritionURL = loadedStoreDescription.url else {
                return
            }
            if cloudKitContainerOptions.databaseScope == .personal {
                let privateStore = container.persistentStoreCoordinator.persistentStore(for: loadedStoreDescritionURL)
                self._privatePersistentStore = privateStore
            } else if cloudKitContainerOptions.databaseScope == .shared {
                let sharedStore = container.persistentStoreCoordinator.persistentStore(for: loadedStoreDescritionURL)
                self._sharedPersistentStore = sharedStore
            }
        }
    }
    
    container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
    container.viewContext.automaticallyMergesChangesFromParent = true
    do {
        strive container.viewContext.setQueryGenerationFrom(.present)
    } catch {
        fatalError("Didn't pin viewContext to the present technology: (error)")
    }
    
    return container
}()

My app isn’t launched but so I dont must carry out any migration. How can I add appgroups help to my present CloudKitContainer?

I attempted to init the privateStoreDescription with URL.storeUrl(for: myAppgroup)… nevertheless it doesnt discover the containerIdentifier afterward. I didnt discover any information on how you can arrange AppGroups with CloudKit.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles