iOS 14 Beta-CloudKit-公共数据库中的coredata共享记录

问题描述

有人能够使用公共数据库使iOS 14 Beta正常工作并保存/共享记录吗?

我尚未将其与Public一起使用。使用他们的代码,我可以创建新记录,并在CloudKit仪表板中查看它们,但我只能看到私有记录。当我添加公共数据库代码时,在本地以外的地方创建时看不到那些记录。 (意思是,我无法在仪表板中看到它们)我是否还需要做一些特殊的事情才能将它们保存在公共数据库中,然后在仪表板中看到它们?我确实看到,当我创建记录并将它们显示在私有数据库中时,它们位于名为com.apple.coredata.cloudkit.zone的区域中。我可以通过仪表板在公共数据库中手动添加记录,但是在手机上运行应用程序时看不到它们。任何建议表示赞赏。谢谢

        
        // Create a container that can load CloudKit-backed stores
        let container = NSPersistentCloudKitContainer(name: "CoreDataCloudKitDemo")
        
        // Enable history tracking and remote notifications
        guard let description = container.persistentStoreDescriptions.first else {
            fatalError("###\(#function): Failed to retrieve a persistent store description.")
        }

        description.cloudKitContainerOptions?.databaseScope = .public
        
        description.setoption(true as NSNumber,forKey: NSPersistentHistoryTrackingKey)
        description.setoption(true as NSNumber,forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)

        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 = appTransactionAuthorName
        
        // Pin the viewContext to the current generation token and set it to keep itself up to date with local changes.
        container.viewContext.automaticallyMergesChangesFromParent = true
        do {
            try container.viewContext.setQueryGenerationFrom(.current)
        } catch {
            fatalError("###\(#function): Failed to pin viewContext to the current generation:\(error)")
        }
        
        // Observe Core Data remote change notifications.
        NotificationCenter.default.addobserver(
            self,selector: #selector(type(of: self).storeRemoteChange(_:)),name: .NSPersistentStoreRemoteChange,object: container)
        
        return container
    }()

解决方法

我知道了。我忘记将两个索引添加到数据库中以使用Public。需要将recordName和ModifiedAt添加到所有recordTypes上的架构中。