观察CKRecord删除通过CKSubscription不起作用 [英] Observe CKRecord deletion via CKSubscription does not work
问题描述
CKSubscription
医生说:的当一个创纪录的修改会导致订阅火,服务器发送推送通知到所有设备与订购的除外一个进行原始更改的记录。的
让我假设有两个设备:装置1
和设备2
来自不同的iCloud帐户登录。让我们假设认购记录删除了一定的记录类型这两个设备。
- 如果
装置1
创建一个记录,然后装置1
删除它,然后设备2
得到通知 - 这是根据DOC,但.. - 如果
装置1
创建一个记录,然后设备2
删除它,然后设备2
得到通知 - 我不认为这是根据DOC,它并没有任何意义,设备2
删除它,装置1
应通知
设置预订上的设备1和设备2
FUNC应用程序(应用程序:UIApplication的,didFinishLaunchingWithOptions launchOptions:[NSObject的:AnyObject]) - GT;布尔{ application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes:.Alert,类别:无))
application.registerForRemoteNotifications() 让defaultContainer = CKContainer.defaultContainer()
让publicDatabase = defaultContainer.publicCloudDatabase publicDatabase.fetchAllSubscriptionsWithCompletionHandler({订阅,错误 如果错误== {为零 如果subscriptions.count == 0 { 让订阅= CKSubscription(RECORDTYPE:OU,predicate:NS predicate(价值:真),选项:.FiresOnRecordDeletion)
subscription.notificationInfo = CKNotificationInfo()
subscription.notificationInfo.shouldBadge = FALSE
subscription.notificationInfo.alertBody =OU删除或更新过的
publicDatabase.saveSubscription(订阅,completionHandler:{认购,错误
如果错误== {为零
}其他{
的println(\\(error.localizedDescription))
}
})
} }其他{
的println(\\(error.localizedDescription))
}
})
返回true
}
设备上创建记录1
@IBAction FUNC addOU(发件人:AnyObject){ VAR defaultContainer = CKContainer.defaultContainer()
VAR publicDatabase = defaultContainer.publicCloudDatabase 让R = CKRecord(RECORDTYPE:OU,recordId所:CKRecordID(recordName:AAAA))
publicDatabase.saveRecord(R,completionHandler:{R2,错误 如果错误== {为零
}其他{
的println(\\(error.localizedDescription))
}
})
}
删除记录设备2
@IBAction FUNC removeOU(发件人:AnyObject){ VAR defaultContainer = CKContainer.defaultContainer()
VAR publicDatabase = defaultContainer.publicCloudDatabase publicDatabase.deleteRecordWithID(CKRecordID(recordName:AAAA),completionHandler:{recordId所,在错误 如果错误== {为零 }其他{
的println(\\(error.localizedDescription))
}
})
}
我仍然认为IT就没有任何意义 CKSubscription
是如何工作的,但作为一个临时的解决办法,我建议首先改变 CKRecord
的 lastModifiedUserRecordID
来谁想要删除的记录,事后才删除记录的用户。
要修改 lastModifiedUserRecordID
你必须为获取它,并没有做任何事情它保存,然后再删除可以来:
@IBAction FUNC removeOU(发件人:AnyObject){ VAR defaultContainer = CKContainer.defaultContainer()
VAR publicDatabase = defaultContainer.publicCloudDatabase publicDatabase.fetchRecordWithID(CKRecordID(recordName:AAAA),completionHandler:{记录,在错误 如果错误== {为零 publicDatabase.saveRecord(记录,completionHandler:{RECORD2,错误 如果错误== {为零 publicDatabase.deleteRecordWithID(CKRecordID(recordName:AAAA),completionHandler:{recordId所,在错误 如果错误== {为零 }其他{
的println(\\(error.localizedDescription))
}
})
}其他{
的println(\\(error.localizedDescription))
}
}) }其他{
的println(\\(error.localizedDescription))
}
})
}
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.
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.
- If
device 1
creates a record and thendevice 1
deletes it thendevice 2
get notified - THAT IS ACCORDING TO THE DOC, BUT .. - If
device 1
creates a record and thendevice 2
deletes it thendevice 2
get notified - I do NOT think it is ACCORDING TO THE DOC, and IT DOES NOT MAKE ANY SENSE,device 2
deleted it sodevice 1
should be notified
SET UP SUBSCRIPTION ON DEVICE 1 AND DEVICE 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
}
CREATE RECORD on DEVICE 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)")
}
})
}
DELETE RECORD ON DEVICE 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)")
}
})
}
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.
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)")
}
})
}
这篇关于观察CKRecord删除通过CKSubscription不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!