Thing Scope and Data

When a thing is registered on Kii Cloud, a dedicated thing scope is created for it. New buckets and KiiObjects will be defined within this scope (See Thing scope for the overview of the thing scope).

The way you manage buckets and KiiObjects in a thing scope is the same as that of other scopes. See "Managing Data" (Android, iOS, JavaScript) to learn more.

Here are examples of creating a new KiiObject in a thing scope.

Android

  • try {
      // Instantiate a thing by vendor thing ID.
      KiiThing thing = KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS");
    
      // Create a bucket.
      KiiBucket thingBucket = thing.bucket("thing_bucket");
    
      // Create a KiiObject.
      KiiObject object = thingBucket.object();
    
      // Set a key-value pair.
      object.set("geo", new GeoPoint(35.710036, 139.811046));
    
      // Save the KiiObject.
      object.save();
    } catch (AppException e) {
      // Handle the error.
    } catch (IOException e) {
      // Handle the error.
    }
  • // Instantiate a thing by vendor thing ID.
    KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS", new KiiCallback<KiiThing>() {
      @Override
      public void onComplete(KiiThing result, Exception e) {
        if (e != null) {
          // Handle the error.
          return;
        }
    
        // Create a bucket.
        KiiBucket thingBucket = result.bucket("thing_bucket");
    
        // Create a KiiObject.
        KiiObject object = thingBucket.object();
    
        // Set a key-value pair.
        object.set("geo", new GeoPoint(35.710036, 139.811046));
    
        // Save the KiiObject.
        object.save(new KiiObjectCallBack() {
          @Override
          public void onSaveCompleted(int token, KiiObject object, Exception exception) {
            if (exception != null) {
              // Handle the error.
              return;
            }
          }
        });
      }
    });

iOS

Swift:

  • let thing : KiiThing
    
    do{
      // Instantiate a thing by vendor thing ID.
      thing = try KiiThing.loadSynchronous(withVendorThingID: "rBnvSPOXBDF9r29GJeGS")
    }catch(let error as NSError){
      // Handle the error.
      return
    }
    
    // Create a bucket.
    let thingBucket = thing.bucket(withName: "thing_bucket")
    
    // Create a KiiObject.
    let object = thingBucket.createObject()
    
    // Set a key-value pair.
    object.setGeoPoint(KiiGeoPoint(latitude: 35.710036, andLongitude: 139.811046), forKey: "geo")
    
    do {
      // Save the KiiObject.
      try object.saveSynchronous()
    } catch let error as NSError {
      // Handle the error.
      return
    }
  • // Instantiate a thing by vendor thing ID.
    KiiThing.load(withVendorThingID: "rBnvSPOXBDF9r29GJeGS") { (thing , error) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
    
      // Create a bucket.
      let thingBucket = thing!.bucket(withName: "thing_bucket")
    
      // Create a KiiObject.
      let object = thingBucket.createObject()
    
      // Set a key-value pair.
      object.setGeoPoint(KiiGeoPoint(latitude: 35.710036, andLongitude: 139.811046), forKey: "geo")
    
      // Save the KiiObject.
      object.save({ (object : KiiObject?, error : Error?) -> Void in
        if error != nil {
          // Handle the error.
          return
        }
      })
    }

Objective-C:

  • NSError* error = nil;
    
    // Instantiate a thing by vendor thing ID.
    KiiThing* thing = [KiiThing loadSynchronousWithVendorThingID:@"rBnvSPOXBDF9r29GJeGS"
                                                           error:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    // Create a bucket.
    KiiBucket* bucket = [thing bucketWithName:@"thing_bucket"];
    
    // Create a KiiObject.
    KiiObject* obj = [bucket createObject];
    
    // Set a key-value pair.
    [obj setGeoPoint:[[KiiGeoPoint alloc] initWithLatitude:35.710036
                                              andLongitude:139.811046]
              forKey:@"geo"];
    
    // Save the KiiObject.
    [obj saveSynchronous:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
  • // Instantiate a thing by vendor thing ID.
    [KiiThing loadWithVendorThingID:@"rBnvSPOXBDF9r29GJeGS"
                              block:^(KiiThing *thing, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    
      // Create a bucket.
      KiiBucket* bucket = [thing bucketWithName:@"thing_bucket"];
    
      // Create a KiiObject.
      KiiObject* obj = [bucket createObject];
    
      // Set a key-value pair.
      [obj setGeoPoint:[[KiiGeoPoint alloc] initWithLatitude:35.710036
                                                andLongitude:139.811046]
                forKey:@"geo"];
    
      // Save the KiiObject.
      [obj save:YES withBlock:^(KiiObject *object, NSError *error) {
        if (error != nil) {
          // Handle the error.
          return;
        }
      }];
    }];

JavaScript

// Instantiate a thing by vendor thing ID.
KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS", {
  success: function(thing) {
    // Create a bucket.
    var thingBucket = thing.bucketWithName("thing_bucket");

    // Create a KiiObject.
    var object = thingBucket.createObject();

    // Set a key-value pair.
    object.set("geo", KiiGeoPoint.geoPoint(35.710036, 139.811046));

    // Save the KiiObject.
    object.save({
      success: function(theObject) {
        // Do something.
      },
      failure: function(theObject, error) {
        // Handle the error.
      }
    });
  },
  failure: function(error) {
    // Handle the error.
  }
});