0

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 ?

Jasper
  • 7,031
  • 3
  • 35
  • 43
user2924482
  • 8,380
  • 23
  • 89
  • 173

1 Answers1

0

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..")
}
Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58
  • hi @EdwinVermeer. I've tried your idea and the predicate works correctly in a tableview, but not with the subscription. I doubt it's my subscription setup because I can get a true predicate to work. My predicate involves CKReference to the current user, where I want to get a notification for comments made by other users on the current user's post - `let predicate = NSPredicate(format: "user != %@ AND userIDForPost = %@", argumentArray: [userRef, userRef])`. userRef is the current user's ID, "user" is ref to other users, userIDForPost is ref to current user. Super appreciative of any help. – Renee Olson Jun 17 '15 at 02:14
  • I added some info to the answer. Do you have that? Besides that, are you loged in to iCloud on the device? – Edwin Vermeer Jun 17 '15 at 06:22
  • Yes, I have those methods in the AppDelegate. And the device is logged into iCloud with another user's account. And then I go into the dashboard and create new records to match the predicate under my own account, which I think is how Apple suggested to do it. I've tried a lot of things over past couple months and I'm starting to wonder if this is a bug. Thanks so much for your help! @EdwinVermeer. – Renee Olson Jun 17 '15 at 16:59