Mikhail S
Mikhail S

Reputation: 4103

CloudKit subscription sometimes does not work

If I try subscribe to CloudKit with this code:

    NSPredicate *truePredicate = [NSPredicate predicateWithValue:YES];
    CKSubscription *itemSubscription = [[CKSubscription alloc] initWithRecordType:RecordType
                                                                        predicate:truePredicate
                                                                          options:CKSubscriptionOptionsFiresOnRecordCreation | CKSubscriptionOptionsFiresOnRecordUpdate | CKSubscriptionOptionsFiresOnRecordDeletion];


    CKNotificationInfo *notification = [[CKNotificationInfo alloc] init];
    notification.alertBody = @"Item Added/Updated/Deleted!";
    itemSubscription.notificationInfo = notification;

    [self.publicDatabase saveSubscription:itemSubscription completionHandler:^(CKSubscription *subscription, NSError *error) {
        if (error) {
            // In your app, handle this error appropriately.
            NSLog(@"An error occured in %@: %@", NSStringFromSelector(_cmd), error);
        } else {

            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:subscription.subscriptionID forKey:kSubscriptionID];
            [defaults synchronize];
        }
    }];

I sometimes get this error:

CKError 0x17558460: "Server Rejected Request" (15/2000); server message = "Internal server error"; uuid = B89DE7A4-9D22-42BC-9CD4-4330F3FE04EF; container ID = "iCloud.com.app.testApp"

or

CKError 0x14fb3510: "Service Unavailable" (6/2022); server message = "failed up to install schema, CAS failed"; uuid = F562D1AD-B40E-4842-A5EA-2A5F800C18F2; container ID = "iCloud.com.app.testApp"

Anybody know how to fix that? Can I do something with my code? Is this Apple problem and I can't do anything? Thanks.

Upvotes: 9

Views: 2488

Answers (3)

AAH
AAH

Reputation: 11

I had exact error : "Server Rejected Request" (15/2000); server message = "Internal server error" as result of CKModifySubscriptionsOperation.

Strange thing that testing the subscription with iPad was Ok. but, subscription from iPhone didn't work.

Fixed it by changing the NSPredicate format:

1- old format (not working)

1-1:

let predicate = NSPredicate(format: "rate >= 0")

1-2:

let x = 0 as! NSNumber

let predicate = NSPredicate(format: "rate >=", x)

2- New predicate format (fixed the issue):

let predicate = NSPredicate(format: "rate >=", NSNumber(integerLiteral: 0))

Upvotes: 1

adam.wulf
adam.wulf

Reputation: 2169

I just recently got a similar error, and was able to resolve it by toggling CloudKit in the project's capabilities. Once I reset that CloudKit permission, all seemed to work just fine.

Upvotes: 3

ninjaneer
ninjaneer

Reputation: 7031

I had the exact same problem. I ended up changing the containers entirely (goto project target -> Capabilities -> specify custom containers -> enter a new container ID). It worked perfectly after.

Upvotes: 1

Related Questions