CKSubscription
doc 说:当记录修改导致订阅触发时,服务器会向所有具有该订阅的设备发送推送通知,但对记录进行原始更改的设备除外。
假设我有两个设备:device 1
并device 2
从不同的 iCloud 帐户登录。假设两个设备都订阅了某个记录类型的记录删除。
- 如果
device 1
创建记录然后将其device 1
删除,则会device 2
收到通知-这是根据文档,但是.. - 如果
device 1
创建记录然后将其device 2
删除然后device 2
收到通知 - 我不认为它是根据文档,并且它没有任何意义,device 2
删除它所以device 1
应该通知
在设备 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)")
}
})
}