Thing スコープとデータ

Kii の Thing 機能では、Thing に対して専用の Thing スコープが作成され、このスコープ内に Bucket に Object が作成されます(Thing スコープの概要は Thing スコープ をご参照ください)。

Bucket や Object に対する操作方法は他のスコープの場合と同様です。詳しい操作方法については「データ管理機能」(AndroidiOSJavaScript)を参照してください。

ここでは一例として、Thing スコープに新たな Object を作成する例を挙げます。

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.
  }
});