CloudKit: query subscriptions request

374 Views Asked by At

I'm working in app using CloudKit and I'm creating a subscription to CloudKit. here is my code:

CKSubscription *subscription = [[CKSubscription alloc]
                                initWithRecordType:recordType
                                predicate:predicate
                                options:CKSubscriptionOptionsFiresOnRecordCreation |
                                CKSubscriptionOptionsFiresOnRecordUpdate |
                                CKSubscriptionOptionsFiresOnRecordDeletion];


CKNotificationInfo *notificationInfo = [CKNotificationInfo new];
notificationInfo.shouldSendContentAvailable = YES;
subscription.notificationInfo = notificationInfo;
notificationInfo.shouldBadge = YES;
CKDatabase *publicDatabase = [container publicCloudDatabase];
[publicDatabase saveSubscription:subscription
               completionHandler:^(CKSubscription *subscription, NSError *error) {
                   if (!error)
                   {
                       NSLog(@"subscription success!");
                   }
                   else
                   {
                       NSLog(@"subscription  error%@", error.localizedDescription);
                   }

               }];

My question to you guys. How can I query or validate the user subscription to CloudKit ?

1

There are 1 best solutions below

3
On

A subscription is nothing more than a CKPredicate that is active on the server side instead of in your app. If you want to validate if the predicate is correct, then just execute it as a query and see what you get back.

Make sure your application difFinishLaunchingWithOptions has the following lines of code:

    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert | .Badge | .Sound, categories: nil))
    application.registerForRemoteNotifications()

Also make sure you handle incoming notifications by adding this:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    NSLog("Push received.. Should be handled..")
}