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

时间:2014-09-13 09:56:59

标签: ios apple-push-notifications ios8 cloudkit cksubscription

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

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

  1. 如果device 1创建了一条记录,然后device 1删除了该记录,则device 2会收到通知 - 这符合DOC,但是......
  2. 如果device 1创建了一条记录,然后device 2将其删除,那么device 2会收到通知 - 我认为这不符合DOC,并且它不会发出任何意见,< strong> device 2已将其删除,因此应通知device 1

  3. 在设备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)")
            }
        })
    }
    

1 个答案:

答案 0 :(得分:9)

我仍然认为IT不会感谢CKSubscription的工作原理,但作为临时修复,我建议先将CKRecord lastModifiedUserRecordID更改为想要的用户删除记录,然后才删除记录。

要更改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)")
        }
    })
}