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

前端之家收集整理的这篇文章主要介绍了ios – 通过CKSubscription观察CKRecord删除不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
CKSubscription doc说:当记录修改导致订阅触发时,服务器会向具有该订阅的所有设备发送推送通知,但对原始更改记录的设备除外.

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

>如果设备1创建了一个记录,然后设备1删除它,那么设备2会收到通知 – 这符合DOC,但是……
>如果设备1创建了一个记录,然后设备2删除了它,那么设备2会收到通知 – 我认为它不符合DOC,并且它没有任何感觉,设备2将其删除,因此应该通知设备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)")
        }
    })
}

解决方法

我仍然认为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"),error in

                        if error == nil {

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

        } else {
            println("\(error.localizedDescription)")
        }
    })
}
原文链接:https://www.f2er.com/iOS/333942.html

猜你在找的iOS相关文章