4

I manage to save, change and delete records in Apples CloudKit. I even got notifications working with subscriptions, what I can not find out is, how do i list all subscriptions for the current user.

Here is my code so far:

let operation = CKFetchSubscriptionsOperation()
operation.fetchSubscriptionCompletionBlock = { (d, e) -> Void in
    println("got subscription")
    if e != nil {
        println("Error")
        dump(e)
    }
    dump(d)
}
publicDatabase.addOperation(operation)

What I got is:

got subscription
Error
- <CKError 0x14db0ed0: "Invalid Arguments" (12)> #0
- 0 key/value pairs

What are the Invalid Arguments? And how do i get a list of all saved subscriptions?

Jojodmo
  • 23,357
  • 13
  • 65
  • 107
Peter Shaw
  • 1,867
  • 1
  • 19
  • 32

2 Answers2

9

If you want to do something with your subscriptions, then you could use something like this:

var database = CKContainer.defaultContainer().publicCloudDatabase
database.fetchAllSubscriptionsWithCompletionHandler({subscriptions, error in
        for subscriptionObject in subscriptions {
            var subscription: CKSubscription = subscriptionObject as CKSubscription
            ..
        }
    }
Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58
  • Thank you very much. These lines save my day, Edwin. – Peter Shaw Nov 29 '14 at 14:23
  • @EdwinVermeer, do yo know how to fetch subscriptions in Objective-C ? – user2924482 Jun 04 '15 at 07:14
  • [privateDatabase fetchAllSubscriptionsWithCompletionHandler:^(NSArray *subscriptions, NSError *error) { NSLog(@"Test"); }]; – Edwin Vermeer Jun 04 '15 at 08:41
  • Then just loop through the array where you now see the NSLog – Edwin Vermeer Jun 04 '15 at 08:42
  • Is it fetching all the subscriptions or all the subscriptions of the current user? – Armand Grillet Aug 20 '15 at 14:26
  • It's fetching all the subscriptions of the current user – Edwin Vermeer Aug 20 '15 at 14:27
  • From my test, corroborated by [the docs](https://developer.apple.com/reference/cloudkitjs/cloudkit.database/1628551-fetchallsubscriptions), this fetches all subscriptions in the schema (i.e. all users). I think you (and I) want to use `fetchAllSubscriptionsOperation`, which can fetch only subscriptions for the current user ([reference](https://developer.apple.com/reference/cloudkit/ckfetchsubscriptionsoperation/1515282-fetchallsubscriptionsoperation)). – Anthony C Oct 29 '16 at 21:00
3

I haven't tried this yet but according to the docs; fetchAllSubscriptionsOperation() will get all subscriptions for the user in the current database, assigning the value of this call to fetchSubscriptionCompletionBlock should then work. Adjust the following line of code to:

let operation = CKFetchSubscriptionsOperation.fetchAllSubscriptionsOperation()

Quick Help Notes

fetchAllSubscriptionsOperation

Again, according to the docs fetchAllSubscriptionsWithCompletionHandler will fetch all subscriptions on the current database:

fetchAllSubscriptionsWithCompletionHandler-help

Tommie C.
  • 12,895
  • 5
  • 82
  • 100