首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CloudKit:删除CKSubscription不起作用

CloudKit:删除CKSubscription不起作用
EN

Stack Overflow用户
提问于 2016-03-15 17:03:46
回答 1查看 489关注 0票数 1

你好,我试图取消对CloudKit的订阅。到目前为止,我尝试了CKModifySubscriptionsOperation和publicDB?.deleteSubscriptionWithID的单重移动。我的completionHandler没有给出错误和一个已删除的订阅ID数组,所以一切看起来都很好。

但是,当我更改记录时,仍会收到推送通知。如何删除订阅?

以下是修改后的代码:

代码语言:javascript
复制
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指示板

订阅时我的日志:

代码语言:javascript
复制
- * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
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

和删除订阅: error = nil

代码语言:javascript
复制
- * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-15 21:00:08

好的,这不是明确的答案,但需要放下一些代码,评论太短了。这意味着什么;第一个方法获取任何未完成的通知,第二个方法确保您的页面与它们的注意相同。

在删除订阅之前,您需要确保得到了类似于place的内容。

免责声明--这是演示代码;我还没有对它进行广泛的测试。但这应该管用。

代码语言:javascript
复制
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)
}


}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36017656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档