通过 CKSubscription 观察 CKRecord 删除不起作用 [英] Observe CKRecord deletion via CKSubscription does not work

查看:26
本文介绍了通过 CKSubscription 观察 CKRecord 删除不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

CKSubscription doc says: When a record modification causes a subscription to fire, the server sends push notifications to all devices with that subscription except for the one that made the original change to the record.

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

Let assume I have two devices: device 1 and device 2 logged in from different iCloud accounts. Let assume both devices subscribed for record deletion for a certain record type.

  1. 如果 device 1 创建记录,然后 device 1 删除它,则 device 2 会收到通知 - 这是根据 DOC,但是..
  2. 如果 device 1 创建一个记录,然后 device 2 删除它然后 device 2 得到通知 - 我不认为这是根据DOC,它没有任何意义,device 2 删除它所以 device 1 应该被通知
  1. If device 1 creates a record and then device 1 deletes it then device 2 get notified - THAT IS ACCORDING TO THE DOC, BUT ..
  2. If device 1 creates a record and then device 2 deletes it then device 2 get notified - I do NOT think it is ACCORDING TO THE DOC, and IT DOES NOT MAKE ANY SENSE, device 2 deleted it so device 1 should be notified

<小时>

在设备 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 给想要删除记录的用户,之后才删除记录.

I still think that IT MAKE NO SENSE how CKSubscription works, but as a temporary fix I recommend to changed first CKRecord's lastModifiedUserRecordID to the user who want to delete the record, and only afterwards to delete record.

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

To change lastModifiedUserRecordID you have to fetch it and without do anything on it save it back, and then deletion can come:

@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)")
        }
    })
}

这篇关于通过 CKSubscription 观察 CKRecord 删除不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆