如何接收有关与我共享的记录所做更改的cloudkit通知? [英] How to receive cloudkit notifications about changes made on record shared with me?

查看:207
本文介绍了如何接收有关与我共享的记录所做更改的cloudkit通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两个不同的设备上有两个icloud帐户( A B )。从其中一个( A )我分享ckrecord到另一个( B ),如下所示:

I have two icloud accounts (A and B) on two different devices. From one of them (A) I share ckrecord to another one (B) like this:

let controller = UICloudSharingController { controller, preparationCompletionHandler in

    let share = CKShare(rootRecord: record)
    share[CKShareTitleKey] = "title" as CKRecordValue

    share[CKShareTypeKey] = "pl.blueworld.fieldservice" as CKRecordValue
    share.publicPermission = .readWrite

    let modifyOperation = CKModifyRecordsOperation(recordsToSave: [record, share], recordIDsToDelete: nil)
    modifyOperation.savePolicy = .ifServerRecordUnchanged
    modifyOperation.perRecordCompletionBlock = { record, error in
        print(error?.localizedDescription ?? "")
    }

    modifyOperation.modifyRecordsCompletionBlock = { records, recordIds, error in

        print(share.url)
        preparationCompletionHandler(share, CloudAssistant.shared.container, error)
    }

    CloudAssistant.shared.container.privateCloudDatabase.add(modifyOperation)
}

controller.delegate = self

UIViewController.top()?.present(controller, animated: true)

当第二个设备( B )接受了cloudkit共享我获取记录并订阅了更改:

When second device (B) did accept cloudkit share I fetch record and subscribe for changes:

func application(_ application: UIApplication, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShareMetadata) {

    let acceptSharesOperation = CKAcceptSharesOperation(shareMetadatas: [cloudKitShareMetadata])
    acceptSharesOperation.perShareCompletionBlock = { metadata, share, error in

        if let error = error {
            UIAlertController.show(withMessage: error.localizedDescription)
        } else {

            let operation = CKFetchRecordsOperation(recordIDs: [cloudKitShareMetadata.rootRecordID])
            operation.perRecordCompletionBlock = { record, _, error in

                if let error = error {
                    UIAlertController.show(withMessage: error.localizedDescription)
                } else if let record = record {
                    CloudAssistant.shared.save(records: [record], recordIDsToDelete: [])

                    let options: CKQuerySubscriptionOptions = [.firesOnRecordCreation, .firesOnRecordUpdate, .firesOnRecordDeletion]
                    let territorySubscription = CKQuerySubscription(recordType: "Territory", predicate: NSPredicate(value: true), options: options)

                    let notificationInfo = CKNotificationInfo()
                    notificationInfo.shouldBadge = false
                    notificationInfo.shouldSendContentAvailable = true

                    territorySubscription.notificationInfo = notificationInfo

                    CloudAssistant.shared.sharedDatabase?.save(territorySubscription) { _, _ in }
                }
            }

            CloudAssistant.shared.container.sharedCloudDatabase.add(operation)
        }
    }
    acceptSharesOperation.qualityOfService = .userInteractive
    CKContainer(identifier: cloudKitShareMetadata.containerIdentifier).add(acceptSharesOperation)
}

现在来自设备 A 成功(我确信,更改会保存在iCloud中)对共享的记录执行更新其他。但设备 B 并不知道这一点,除非我再次手动获取记录。

Now from device A I sucessfully (I am sure about that, changes is saved in iCloud) perform an update on a record shared with others. But device B doesnt know about that, unless I fetch record manually once again.

但从另一方面来看,它效果很好好吧。

如果我在与我共享的记录上成功执行更新(在设备上 B ),那么设备一个 mnagically得到关于变化的通知,一切都很好。有什么区别?

If I successfully perform an update on a record shared with me (on device B) then device A mnagically gets a notification about change and everything is fine. What makes the difference?

如何订阅与我共享的记录的更改?

How to subscribe for changes on a records shared with me?

PS当我我将能够为那些帮助我的人开始200或更多的数量。

P.S when I will be able I will start a bount of 200 or even more for someone who will help me with this.

iOS 11,Swift 4,Xcode 9

iOS 11, Swift 4, Xcode 9

推荐答案

这是我的调试订阅通知清单没有按预期显示。听起来你可能已经排除了其中一些。

Here's my checklist for debugging subscription notifications not appearing as expected. Sounds like you may have ruled some of these out already.


  1. 确保应用已注册通知

  2. 确保在此设备的设备上启用了此应用的通知

  3. 确保所有设备都使用相同的容器

  4. 在下一个应用启动时,使用 fetchAllSubscriptionsWithCompletionHandler 和NSLog读取所有订阅每个子的详细信息(尤其是:subscriptionID,触发器选项,记录类型和谓词)。验证预期的子存在。验证每个子谓词是否符合预期(在这种情况下,比较您在两个设备上找到的谓词)。

  1. Make sure the app is registered for notifications
  2. Make sure notifications are enabled on the device for this app
  3. Make sure all devices are using the same container
  4. On the next app startup, read all subscriptions using fetchAllSubscriptionsWithCompletionHandler and NSLog each sub's details (especially: subscriptionID, trigger options, record type and the predicate). Verify that the expected subs exist. Verify that each sub's predicate matches expectations (in this case, compare the predicates you find on both devices).

我浪费了一堆在以下情况下调试丢失通知的时间:

I wasted a bunch of time debugging "missing" notifications when:


  1. 我的本地构建版本使用TEST环境并且TestFlight用户正在访问PROD环境。调试步骤2发现了这一点。

  2. 当无意中重新使用订阅ID时,每个新子包都覆盖了前一个。调试步骤4最终揭示了问题

到目前为止,这四个调试步骤帮助我理解了所有遗漏通知问题。一旦我明白为什么没有出现通知,就缩小了代码块的负责范围。

So far, these four debugging steps have helped me understand all of my "missing notification" problems. Once I understood why the notifs didn't appear, that narrowed down which block of code was responsible.

这篇关于如何接收有关与我共享的记录所做更改的cloudkit通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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