Search code examples
ios8cloudkitcksubscription

CloudKit. Problems with subscriptions


I do not quite understand the information about subscriptions. I have Private Database. There is a table to which I create a subscription. If I subscribe to two devices under one iCloud account, errors occur: Subscribed with error:

CKError 0x165c6ac0: "Server Rejected Request" (15/2032); server message = "subscription is duplicate of 'C6051A24-2DB5-47EA-98D2-7D8786AA6D3B'"; uuid = 48AC7472-006A-4FD7-84A9-DB342C83C199; container ID = "iCloud.com. *. *"

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


CKNotificationInfo *notification = [[CKNotificationInfo alloc] init];
notification.shouldSendContentAvailable = YES;

notification.alertLocalizationKey = @"";
notification.shouldBadge = NO;

itemSubscription.notificationInfo = notification;

[self.privateDatabase saveSubscription:itemSubscription completionHandler

Maybe I'm doing something wrong? What I need to do to both devices receive notification of subscriptions?

Edited:

Also. I create only one subscription. If I use method fetchAllSubscriptionsWithCompletionhandler I see 3-4 of the subscriptions per table. But it's weird because I create only one. And when I receive the notification, I see more notifications than it should be.


Solution

  • A subscription is per Database. So if you want to receive notifications to multiple users on a database it is enough to create it once. You are getting an error because you are creating the same subscription (same subscription ID) twice on the same database. Simply make sure you create it only once. You can fetch subscriptions to check if it has already been created or not. Harry