您需要在应用启动时创建对共享数据库的订阅。这样,当用户(在任何设备上)接受共享时,订阅将被触发,并且该用户的所有设备(具有订阅)将拉取该数据库中的最新数据。没有必要CKShare
在多个设备上接受。
下面是一个如何创建共享数据库订阅的示例:
let container = CKContainer(identifier: "...")
let subscription = CKDatabaseSubscription(subscriptionID: "shared")
let sharedInfo = CKNotificationInfo()
sharedInfo.shouldSendContentAvailable = true
sharedInfo.alertBody = "" //This needs to be set or pushes sometimes don't get sent
subscription.notificationInfo = sharedInfo
let operation = CKModifySubscriptionsOperation(subscriptionsToSave: [subscription], subscriptionIDsToDelete: nil)
container.sharedCloudDatabase.add(operation)
您还需要处理从共享数据库中获取的更改,以在您的应用中显示新的共享数据,如下所示:
let fetchOperation = CKFetchDatabaseChangesOperation()
fetchOperation.fetchAllChanges = true
fetchOperation.recordZoneWithIDChangedBlock = { recordZoneID in
//Process custom/shared zone changes...
}
container.sharedCloudDatabase.add(fetchOperation)
我希望这会有所帮助。