iosobjective-ccloudkitcksubscription

How do i delete cksubscription in Objective-c?


I'm sorry, but I can't find the answer in plain English, or at least every answer I see assumes I have a certain amount of knowledge that must not have. I just need to delete a CKSubscription. How do I do this? All tips and help will be greatly appreciated.


Solution

  • Assuming you don't have the subscriptionID of the subscription you want to delete, you should first fetch all of the subscriptions and figure out the subscriptionID of the subscription you want to delete:

    [[[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
        }
    }];
    

    Then, now that you have the subscriptionID, simply delete it:

    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];
    

    Don't forget to always handle all the possible errors properly.