通过CKSubscription观察CKRecord删除不起作用
CKSubscription
文档说: 当一个logging修改导致一个订阅触发,服务器发送推送通知给所有具有该订阅的设备, 除了原来的更改logging。
假设我有两个设备: device 1
和device 2
从不同的iCloud帐户login。 假设两个设备都订阅了某个loggingtypes的logging删除。
- 如果
device 1
创buildlogging,然后device 1
删除它,则device 2
得到通知 – 这是根据DOC,但.. - 如果
device 1
创build一个logging,然后device 2
删除它,然后device 2
得到通知 – 我不认为这是根据DOC,并且它没有任何感觉,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上创buildlogging
@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)") } }) }
在设备上删除logging2
@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
工作,但作为一个临时修复,我build议将第一个CKRecord
的lastModifiedUserRecordID
更改为想要删除该logging的用户,并且只有后来删除logging。
要改变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)") } }) }