阅读背景:

努力理解CloudKit中的CKSubscriptions

来源:互联网 

I've been reading about and trying to use the CKSubscription feature for weeks, but I can't find info about some general questions. I've read Apple docs, online tutorials, books I bought, and questions here on SO, but I still don't understand the fundamentals I think. Any help is very much appreciated.

我已经阅读并尝试使用CKSubscription功能数周,但我找不到一些常见问题的信息。我已经阅读了Apple文档,在线教程,我购买的书籍以及SO上的问题,但我仍然不理解我认为的基本原理。很感谢任何形式的帮助。

Here are questions I cannot find answers to:

以下是我无法找到答案的问题:

1). What is the purpose of the subscriptionID? The convenience init does not include it, so why is it needed in the designated init? If you use it, is it the same for all users of the app?

1)。 subscriptionID的目的是什么?方便init不包含它,那么为什么在指定的init中需要它呢?如果您使用它,对于应用的所有用户来说它是否相同?

2). I saw someone mention here that you can unregister a subscriptionID. Why or how would you do this?

2)。我看到有人在这里提到你可以取消注册subscriptionID。为什么或如何做到这一点?

3). Can subscriptions be setup in both public or the user's private database?

3)。可以在公共或用户的私人数据库中设置订阅吗?

4). If I have a query based subscription that is the same for all users, will there only ever be 1 subscription listed in the database?

4)。如果我有一个基于查询的订阅对所有用户都相同,那么数据库中是否只列出了1个订阅?

For instance, I'm having trouble getting notifications to work with my specific use case. It's not a problem in my setup, as I can get a True predicate to work and the notification comes. So I must not understand the fundamentals of how subscriptions work still.

例如,我无法收到通知以使用我的特定用例。这在我的设置中不是问题,因为我可以获得True谓词工作并且通知到来。所以我不能理解订阅如何工作的基本原理。

I'm trying to setup a subscription that fires whenever a new record is created when a different user makes a comment on a post. This new record will then contain a reference to the user who created the post. The only subscription I see in the database for both users is - Notifications.user (equals reference). So, I'm assuming I'll only ever see this one subscription.(?) But how does the server keep track of every user's recordID or know when to send it to a specific device?

我正在尝试设置一个订阅,当其他用户对帖子发表评论时,只要创建新记录就会触发该订阅。然后,此新记录将包含对创建帖子的用户的引用。我在数据库中为两个用户看到的唯一订阅是 - Notifications.user(等于引用)。所以,我假设我只会看到这一个订阅。(?)但是服务器如何跟踪每个用户的recordID或知道何时将其发送到特定设备?

The problem is I can't get the notification to work. I manually add a record in the dashboard, and I put the other user's recordID as the CKReference. While I'm adding the record, I have the app running in the background on a device under the user's account whom I added as the CKReference in the field. I'd expect the query to trigger and send a push notification since someone commented on this user's post.

问题是我无法通知工作。我在仪表板中手动添加记录,并将其他用户的recordID作为CKReference。当我添加记录时,我将应用程序在后台运行在用户帐户下的设备上,我在该字段中添加了CKReference。我希望查询能够触发并发送推送通知,因为有人对此用户的帖子发表了评论。

Here's my code to setup the subscription:

这是我设置订阅的代码:

func subscribe(userID: CKRecordID) {


    let options = CKSubscriptionOptions.FiresOnRecordCreation

    let userRef = CKReference(recordID: userID, action: .DeleteSelf)
    let predicate = NSPredicate(format: "userRef == %@", userRef)
    let predicateTwo = NSPredicate(format: "read = %@", "")
    let compoundPred = NSCompoundPredicate(type: .AndPredicateType, subpredicates: [predicate, predicateTwo])

    let subscription = CKSubscription(recordType: "Notifications", predicate: compoundPred, subscriptionID: subscriptionID,
        options: options)
    subscription.notificationInfo = CKNotificationInfo()
    subscription.notificationInfo.desiredKeys = ["userPost"]
    subscription.notificationInfo.alertBody = "Someone replied to your Pod post"

    publicDB.saveSubscription(subscription, completionHandler: {

        subscription, error in

        if (error != nil) {

            println("error subscribing: \(error)")

        } else {

            println("subscribed!")

        }

    })
}

1 个解决方案

#1


Let me try and answer:

让我试着回答:

  1. SubscriptionId allows you to identify later a subscription for example to delete it using the CKDatabase method deleteSubscriptionWithID
  2. SubscriptionId允许您稍后识别订阅,例如使用CKDatabase方法deleteSubscriptionWithID将其删除

  3. For how see answer 1. As to why, well maybe you do not want to get notifications on this subscription any more. This depends on what you are trying to achieve with your app.
  4. 如何看答案1.至于为什么,也许你不想再收到有关此订阅的通知。这取决于您尝试使用您的应用实现的目标。

  5. Yes
  6. Yes, if you register only one subscription it should work for all users of your app
  7. 是的,如果您只注册一个订阅,它应该适用于您应用的所有用户

Regarding your issues, please note that a user recordIDs are special, so you may have issues specifically related to that due to the privacy issues around them. I would suggest to try a simple case that does not involve users and see if subscriptions are working for you. Then think again about how you use user record IDs

关于您的问题,请注意用户记录ID是特殊的,因此由于周围的隐私问题,您可能会遇到与此相关的问题。我建议尝试一个不涉及用户的简单案例,看看订阅是否适合你。然后再考虑一下如何使用用户记录ID


分享到: