CloudKit subscription sometimes does not work
Asked Answered
B

3

9

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.

Bookbinder answered 29/9, 2014 at 5:10 Comment(3)
I've had the first error before, where it just appear suddenly for a few hours, and went away after that. That was problem with Apple's server. But if the problem does not go away, then it is probably yours.Are
for the second error, have you already created the record type before you subscribe to it?Are
yes, I have record type.Bookbinder
B
3

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.

Bruni answered 21/10, 2015 at 22:59 Comment(2)
I was having the same issue and while a new container did not fix it, this suggestion appeared to fix it. However, the problem has come back a few times. I might try creating a clean Xcode project.Dilettante
This also solved a somewhat similar CKInternalErrorDomain complaint about the schema not matching with the server.Gumption
R
1

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.

Regal answered 19/10, 2014 at 20:8 Comment(2)
Do you think the whole thing in default container?Bookbinder
I'm not sure what you're asking.Regal
S
1

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))

Shadoof answered 25/12, 2016 at 12:39 Comment(1)
The question was asking for Objective-C. Can you revise your answer?Smolensk

© 2022 - 2024 — McMap. All rights reserved.