如何在Objective-c中删除cksubscription?

时间:2016-06-04 22:09:07

标签: ios objective-c cloudkit cksubscription

我很抱歉,但是我用简单的英语找不到答案,或者至少我看到的每个答案都假定我有一定数量的知识是绝对不能的。我只需要删除CKSubscription。我该怎么做呢?所有提示和帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

假设您没有要删除的订阅的订阅ID,您应首先获取所有订阅并找出您要删除的订阅的订阅ID:

[[[CKContainer defaultContainer] publicCloudDatabase] fetchAllSubscriptionsWithCompletionHandler:^(NSArray<CKSubscription *> * _Nullable subscriptions, NSError * _Nullable error) {
    if (!error) {
        //do your logic to find out which subscription you want to delete, and get it's subscriptionID
    } else {
        //handle error
    }
}];

然后,既然你有了subscriptionID,只需删除它:

CKModifySubscriptionsOperation *operation = [[CKModifySubscriptionsOperation alloc] initWithSubscriptionsToSave:nil subscriptionIDsToDelete:YOUR_SUBSCRIPTION_ID];

operation.modifySubscriptionsCompletionBlock = ^(NSArray <CKSubscription *> * __nullable savedSubscriptions, NSArray <NSString *> * __nullable deletedSubscriptionIDs, NSError * __nullable operationError) {
    //handle the results when the operation has completed
};

[[[CKContainer defaultContainer] publicCloudDatabase] addOperation:operation];

不要忘记始终正确处理所有可能的错误。