CloudKit:删除 CKSubscription 不起作用
Posted
技术标签:
【中文标题】CloudKit:删除 CKSubscription 不起作用【英文标题】:CloudKit: delete CKSubscription is not working 【发布时间】:2016-03-15 17:03:46 【问题描述】:您好,我尝试取消对 CloudKit 的订阅。 到目前为止,我尝试了 CKModifySubscriptionsOperation 和 publicDB?.deleteSubscriptionWithID 的单一删除。我的 completionHandler 没有给我任何错误,并且有一个已删除订阅 ID 的数组。所以一切似乎都很好。
但是当我更改记录时,我仍然会收到推送通知。 如何删除订阅?
这是修改后的代码:
func cancelCubscription(subscription:Subscriptions)
self._animationDelegate?.beginAnimation()
var subs = [String]()
subs.append(subscription.daySubSignature!)
subs.append(subscription.hdpSubSignature!)
subs.append(subscription.noteSubSignature!)
subs.append(subscription.planerSubSignature!)
subs.append(subscription.subjectSubSugnature!)
subs.append(subscription.teacherSubSignature!)
subs.append(subscription.tldSubSignature!)
// Create a CKModifyRecordsOperation operation
let subscriptionOperation = CKModifySubscriptionsOperation(subscriptionsToSave: nil, subscriptionIDsToDelete: subs)
subscriptionOperation.modifySubscriptionsCompletionBlock = (modifiedSubscriptions: [CKSubscription]?, deletedSubscriptionIDs: [String]?, error: NSError?) -> Void in
guard error==nil else
dispatch_async(dispatch_get_main_queue(), () -> Void in
self._animationDelegate?.endAnimation()
print(error!.localizedDescription)
let title:String = "Alert_Cancel_CloudKit_Subscription_Error_Title".localized
let message:String = error!.localizedDescription
self._alertDelegate?.showAlert(title, message: message)
)
return
dispatch_async(dispatch_get_main_queue(), () -> Void in
self._animationDelegate?.endAnimation()
print("")
print("- * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *")
print("Unscribe to CloudKit subscription success!")
let title:String = "Alert_Cancel_CloudKit_Subscription_Success_Title".localized
let message:String = "Alert_Cancel_CloudKit_Subscription_Success_Message".localized
self._alertDelegate?.showAlert(title, message: message)
for sub in deletedSubscriptionIDs!
print("Deleted Subscription: \(sub)")
subscription.tldSubSignature = "null"
subscription.daySubSignature = "null"
subscription.hdpSubSignature = "null"
subscription.noteSubSignature = "null"
subscription.planerSubSignature = "null"
subscription.subjectSubSugnature = "null"
subscription.teacherSubSignature = "null"
subscription.subscribed = false
Subscriptions.editSubsripction(subscription, context: self.appDel.managedObjectContext)
)
// Add the operation to an operation queue to execute it
self._publicDB!.addOperation(subscriptionOperation)
我的订阅仍然有效: CloudKit Dashboard
我订阅时的日志:
- * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
CloudKit subscription success
teacherSubSignature = DE49AAFB-373C-49E5-8DC8-E81A977E7628
subjectSubSugnature = 3F06E054-D1B9-441E-AED3-CA947820D90C
daySubSignature = 8FF9AC02-F177-4381-AA8D-08AFFDB0A578
hdpSubSignature = 811ABA9A-D31B-4661-B783-B5DF033FC5F4
tldSubSignature = 1C07C661-02A9-48F3-9B92-08F41433E179
planerSubSignature = 1C32F1F0-3647-4F57-BDEF-215A28F69039
hdpSubSignature = 59216B52-1A21-4666-950A-F8CDE44591D2
Subscription Edited in Core Data
在删除订阅时: 错误=无
- * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
Unscribe to CloudKit subscription success!
Deleted Subscription: 8FF9AC02-F177-4381-AA8D-08AFFDB0A578
Deleted Subscription: 59216B52-1A21-4666-950A-F8CDE44591D2
Deleted Subscription: 811ABA9A-D31B-4661-B783-B5DF033FC5F4
Deleted Subscription: 1C32F1F0-3647-4F57-BDEF-215A28F69039
Deleted Subscription: 3F06E054-D1B9-441E-AED3-CA947820D90C
Deleted Subscription: DE49AAFB-373C-49E5-8DC8-E81A977E7628
Deleted Subscription: 1C07C661-02A9-48F3-9B92-08F41433E179
Subscription Edited in Core Data
【问题讨论】:
您是如何删除订阅的?你检查过你有多少订阅(重复)? 我没有重复我的 CloudKit 仪表板仅显示正确的类型和数量。但仍然在删除操作之后。 您是否检查以确保没有待处理的通知?你确定了吗,看答案。 【参考方案1】:好的,这不是最终答案,但需要写一些代码,而且 cmets 太短了。这是什么意思呢;第一种方法获取任何未完成的通知,第二种方法确保您与他们关注的页面相同。
在理想情况下删除订阅之前,您需要确保获得类似位置的信息。
免责声明这是演示代码;而且我还没有对它进行广泛的测试。但它应该可以工作。
func fetchSubsInPlace()
let container = CKContainer(identifier: "iCloud.ch")
let publicDB = container.publicCloudDatabase
publicDB.fetchAllSubscriptionsWithCompletionHandler(subscriptions, error in
for subscriptionObject in subscriptions!
let subscription: CKSubscription = subscriptionObject as CKSubscription
print("subscription \(subscription)")
)
func fetchNotificationChanges()
let operation = CKFetchNotificationChangesOperation(previousServerChangeToken: nil)
var notificationIDsToMarkRead = [CKNotificationID]()
operation.notificationChangedBlock = (notification: CKNotification) -> Void in
// Process each notification received
if notification.notificationType == .Query
let queryNotification = notification as! CKQueryNotification
let reason = queryNotification.queryNotificationReason
let recordID = queryNotification.recordID
print("reason \(reason)")
print("recordID \(recordID)")
// Do your process here depending on the reason of the change
// Add the notification id to the array of processed notifications to mark them as read
notificationIDsToMarkRead.append(queryNotification.notificationID!)
operation.fetchNotificationChangesCompletionBlock = (serverChangeToken: CKServerChangeToken?, operationError: NSError?) -> Void in
guard operationError == nil else
// Handle the error here
return
// Mark the notifications as read to avoid processing them again
let markOperation = CKMarkNotificationsReadOperation(notificationIDsToMarkRead: notificationIDsToMarkRead)
markOperation.markNotificationsReadCompletionBlock = (notificationIDsMarkedRead: [CKNotificationID]?, operationError: NSError?) -> Void in
guard operationError == nil else
// Handle the error here
return
let operationQueue = NSOperationQueue()
operationQueue.addOperation(markOperation)
let operationQueue = NSOperationQueue()
operationQueue.addOperation(operation)
【讨论】:
这听起来不错。因为我观察到我有时会得到不止一次的推动。似乎我有prnding的 好的,现在看起来很好。我不知道我在测试设置期间进行了多少订阅,但是在重置部署环境后,它似乎也可以正常工作,而无需将通知标记为要读取。 THX 为所有答案以上是关于CloudKit:删除 CKSubscription 不起作用的主要内容,如果未能解决你的问题,请参考以下文章