gpt4 book ai didi

ios - 通过 CKSubscription 观察 CKRecord 删除不起作用

转载 作者:可可西里 更新时间:2023-11-01 03:08:57 25 4
gpt4 key购买 nike

CKSubscription 文档说:当记录修改导致订阅触发时,服务器会向所有订阅该订阅的设备发送推送通知,但进行原始更改的设备除外 记录。

假设我有两台设备:device 1device 2 从不同的 iCloud 帐户登录。假设两个设备都订阅了某种记录类型的记录删除。

  1. 如果 device 1 创建了一条记录,然后 device 1 删除了它,然后 device 2 得到通知 - 这是根据文档,但是..
  2. 如果 device 1 创建了一条记录,然后 device 2 删除了它,那么 device 2 会收到通知 - 我不认为这是根据该文档没有任何意义,device 2 删除了它,因此应通知 device 1

在设备 1 和设备 2 上设置订阅

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert, categories: nil))
application.registerForRemoteNotifications()

let defaultContainer = CKContainer.defaultContainer()
let publicDatabase = defaultContainer.publicCloudDatabase

publicDatabase.fetchAllSubscriptionsWithCompletionHandler({subscriptions, error in

if error == nil {

if subscriptions.count == 0 {

let subscription = CKSubscription(recordType: "OU", predicate: NSPredicate(value: true), options: .FiresOnRecordDeletion)
subscription.notificationInfo = CKNotificationInfo()
subscription.notificationInfo.shouldBadge = false
subscription.notificationInfo.alertBody = "OU removed or upated"
publicDatabase.saveSubscription(subscription, completionHandler: {subscription, error in
if error == nil {
} else {
println("\(error.localizedDescription)")
}
})
}

} else {
println("\(error.localizedDescription)")
}
})


return true
}

在设备 1 上创建记录

@IBAction func addOU(sender: AnyObject) {

var defaultContainer = CKContainer.defaultContainer()
var publicDatabase = defaultContainer.publicCloudDatabase

let r = CKRecord(recordType: "OU", recordID: CKRecordID(recordName: "aaaa"))
publicDatabase.saveRecord(r, completionHandler: { r2, error in

if error == nil {
} else {
println("\(error.localizedDescription)")
}
})
}

删除设备 2 上的记录

@IBAction func removeOU(sender: AnyObject) {

var defaultContainer = CKContainer.defaultContainer()
var publicDatabase = defaultContainer.publicCloudDatabase

publicDatabase.deleteRecordWithID(CKRecordID(recordName: "aaaa"), completionHandler: {recordID, error in

if error == nil {

} else {
println("\(error.localizedDescription)")
}
})
}

最佳答案

我仍然认为 CKSubscription 的工作原理毫无意义,但作为临时修复,我建议先更改 CKRecordlastModifiedUserRecordID给想要删除记录的用户,然后才删除记录。

要更改 lastModifiedUserRecordID,您必须获取它并且不做任何操作将其保存回来,然后可以删除:

@IBAction func removeOU(sender: AnyObject) {

var defaultContainer = CKContainer.defaultContainer()
var publicDatabase = defaultContainer.publicCloudDatabase

publicDatabase.fetchRecordWithID(CKRecordID(recordName: "aaaa"), completionHandler: {record, error in

if error == nil {

publicDatabase.saveRecord(record, completionHandler: {record2, error in

if error == nil {

publicDatabase.deleteRecordWithID(CKRecordID(recordName: "aaaa"), completionHandler: {recordID, error in

if error == nil {

} else {
println("\(error.localizedDescription)")
}
})
} else {
println("\(error.localizedDescription)")
}
})

} else {
println("\(error.localizedDescription)")
}
})
}

关于ios - 通过 CKSubscription 观察 CKRecord 删除不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25822019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com