我正在尝试为包含带有用户CKReference的字段的记录设置一个CKSubscription。但是,只要创建了一条记录,它就会忽略compoundPredicate的这一部分,并且永远不会收到通知。在CKSubscription的谓词中使用CKReference有什么不同吗?我进入仪表板在我自己的用户recordID下输入一个新记录(同时在模拟器中运行另一个用户),因为我相信我读到如果记录来自设备,它将不会收到通知。任何见解都非常感谢,因为我已经坚持了一个星期,在网上找不到任何与此相关的东西。我能够获得真类型谓词通知,所以我认为我的基本设置是可以的。
在仪表板中,我看到两个测试用户都有一个通用的订阅,但没有任何特定的recordID (这有关系吗?):
Notifications.read (equals string)
Notifications.user (equals reference)当我执行fetchAllSubscriptionsWithCompletionHandler方法时,它确实将当前用户对此设备的特定recordID显示为调试器中的CKReference。所以我不知道为什么它不能工作。
下面是我的代码,我首先创建CKReference,然后将其用于我的谓词:
var recordIDString = CKRecordID(recordName: "_a86dac8ee05f8c6ab35746bf9663d7b8")
// I normally store this string in the NSUserDefaults.
let userRef = CKReference(recordID: recordIDString, action: .DeleteSelf)
let predicate = NSPredicate(format: "user = %@", userRef)
let predicateTwo = NSPredicate(format: "read = %@", "")
// I want the read field to be empty, which tells me user has not read my internal app notif.
let compoundPred = NSCompoundPredicate(type: .AndPredicateType, subpredicates: [predicate, predicateTwo])
Now I set-up the subscription like this:
let subscription = CKSubscription(recordType: "Notifications",
predicate: compoundPred,
options: .FiresOnRecordCreation)
let notificationInfo = CKNotificationInfo()
notificationInfo.alertBody = "Someone replied to your post"
notificationInfo.shouldBadge = true
subscription.notificationInfo = notificationInfo
publicDB.saveSubscription(subscription,
completionHandler: ({subscription, error in.....etc})
// handling this part发布于 2017-02-03 12:45:22
我也遇到了同样的问题,在苹果网站上偶然发现了这条信息:
创建其谓词包含CKReference字段的订阅时,请确保在创建谓词时使用CKRecordID对象作为该字段的值。在这种情况下使用CKRecord对象目前不起作用。
当我将我的CKQuerySubscription的谓词更改为使用CKRecordID而不是CKReference时,它按预期工作。我意识到这是一个古老的主题,但我很难找到这个问题的答案,因为谓词在CKQuery和CKQuerySubscription中的行为似乎不同。
发布于 2015-05-14 04:58:34
使用NSPredicate而不是NSCompoundPredicate初始化订阅
let predicate = NSPredicate(format: "user = %@ AND read = %@",
argumentArray: [userRef, ""])
let subscription = CKSubscription(recordType: "Notifications",
predicate: predicate,
options: .FiresOnRecordCreation)在我使用CloudKit的经验中,这应该是可行的。
https://stackoverflow.com/questions/29786016
复制相似问题