Subscribing to a Bucket

A user who is interested in changes to a certain bucket can receive a push notification when a change occurs in the bucket by subscribing to the bucket.

Here is the sample code for subscribing to a bucket.

Swift:

  • // If the bucket is new, save it on Kii Cloud by saving a KiiObject in the bucket and then
    // subscribe to the bucket. You cannot subscribe to a bucket if it does not exist on Kii Cloud.
    
    // Instantiate a bucket.
    let bucket = Kii.bucket(withName: "_target_bucket_")
    
    // Create a KiiObject in the bucket.
    let obj1 = bucket.createObject()
    
    do{
      // Save the KiiObject.
      try obj1.saveSynchronous()
    
      // Subscribe to the bucket.
      try KiiUser.current()!.pushSubscription().subscribeSynchronous(bucket)
    } catch let error as NSError {
      // Handle the error.
      return
    }
  • // If the bucket is new, save it on Kii Cloud by saving a KiiObject in the bucket and then
    // subscribe to the bucket. You cannot subscribe to a bucket if it does not exist on Kii Cloud.
    
    // Instantiate a bucket.
    let bucket = Kii.bucket(withName: "_target_bucket_")
    
    // Create and save a KiiObject in the bucket.
    let obj1 = bucket.createObject()
    obj1.save { (retObject : KiiObject?, error : Error?) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
    
      // Subscribe to the bucket.
      KiiUser.current()!.pushSubscription().subscribe(bucket, block: { (subscription : KiiPushSubscription, error : Error?) -> Void in
        if error != nil {
          // Handle the error.
          return
        }
      })
    }

Objective-C:

  • // If the bucket is new, save it on Kii Cloud by saving a KiiObject in the bucket and then
    // subscribe to the bucket. You cannot subscribe to a bucket if it does not exist on Kii Cloud.
    
    NSError *error = nil;
    
    // Instantiate a bucket.
    KiiBucket *bucket = [Kii bucketWithName:@"_target_bucket_"];
    
    // Create and save a KiiObject in the bucket.
    KiiObject *obj1 = [bucket createObject];
    [obj1 saveSynchronous:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    // Subscribe to the bucket.
    [[KiiUser currentUser].pushSubscription subscribeSynchronous:bucket
                                                           error:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
  • // If the bucket is new, save it on Kii Cloud by saving a KiiObject in the bucket and then
    // subscribe to the bucket. You cannot subscribe to a bucket if it does not exist on Kii Cloud.
    
    // Instantiate a bucket.
    KiiBucket *bucket = [Kii bucketWithName:@"_target_bucket_"];
    
    // Create and save a KiiObject in the bucket.
    KiiObject *obj1 = [bucket createObject];
    [obj1 saveWithBlock:^(KiiObject *object, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    
      // Subscribe to the bucket.
      [[KiiUser currentUser].pushSubscription subscribe:bucket
                                                  block:^(KiiPushSubscription *subscription, NSError *error) {
        if (error != nil) {
          // Handle the error.
          return;
        }
      }];
    }];

Subscribing to a bucket with many KiiObjects

A "Push to App" notification will be launched every time there is an update event in the target bucket. Subscribing to a bucket with many Objects, therefore, might cause some issues.

For example, let us assume that a user having three devices is subscribing to a bucket. This bucket has 100 Objects inside. If all of them are updated, it will trigger 100 push notifications per device or 300 total push notifications for this user.

One way to cope with such a situation (massive amount of push notifications triggered) is to use the Push to User Notification instead of the "Push to App" notifications. By sending a push request to a topic after the object updates are done, you can just send one "push to user" message per device. A shortcoming of this method is that there will be a time gap between the object updates and the push notification delivery, and this gap might cause some notifications to be dropped (See Transactions for the related discussion).