Retrieving and Saving/Restoring Things

A thing will have two types of identifiers; a vendorThing ID (assigned by the thing vendor/developer) and thingID (assigned by Kii Cloud). You can retrieve the thing using one of these identifiers.

If you are developing on Android, you can save a thing as a Bundle. Later you can use the Bundle to restore the thing (e.g., when a new Activity is launched).

Retrieving and Saving Things

Retrieving with vendorThingID

The following sample code is retrieving a thing with its vendorThingID.

Android

  • try {
      // Instantiate a thing by vendor thing ID.
      KiiThing thing = KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS");
    
      // Create a bundle and save the thing.
      Bundle bundle = new Bundle();
      bundle.putParcelable("my_thing", thing);
    } 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 bundle and save the thing.
        Bundle bundle = new Bundle();
        bundle.putParcelable("my_thing", thing);
      }
    });

In this sample code, we are saving the things in a Bundle so that it can be restored later. The thing instance is implementing the Parcelable, so you can save it into a Bundle. Plase also check Restoring Things

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
    }
  • // Instantiate a thing by vendor thing ID.
    KiiThing.load(withVendorThingID: "rBnvSPOXBDF9r29GJeGS") { (thing , 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;
    }
  • // Instantiate a thing by vendor thing ID.
    [KiiThing loadWithVendorThingID:@"rBnvSPOXBDF9r29GJeGS"
                              block:^(KiiThing *thing, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    }];

JavaScript

// Instantiate a thing by vendor thing ID.
KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS", {
  success: function(thing) {
    // Do something.
  },
  failure: function(error) {
    // Handle the error.
  }
});

Retrieving with thingID

The following sample code is retrieving a thing with its thingID.

Android

  • try {
      // Instantiate a thing by thing ID.
      KiiThing thing = KiiThing.loadWithThingID("78924c8vy2984298vvc2");
    
        // Create a bundle and save the thing.
        Bundle bundle = new Bundle();
        bundle.putParcelable("my_thing", thing);
    } catch (AppException e) {
      // Handle the error.
    } catch (IOException e) {
      // Handle the error.
    }
  • // Instantiate a thing by thing ID.
    KiiThing.loadWithThingID("78924c8vy2984298vvc2", new KiiCallback<KiiThing>() {
      @Override
      public void onComplete(KiiThing result, Exception e) {
        if (e != null) {
          // Handle the error.
          return;
        }
    
        // Create a bundle and save the thing.
        Bundle bundle = new Bundle();
        bundle.putParcelable("my_thing", thing);
      }
    });

In this sample code, we are saving the things in a Bundle so that it can be restored later. The thing instance is implementing the Parcelable, so you can save it into a Bundle. Plase also check Restoring Things

iOS

Swift:

  • let thing : KiiThing
    
    do{
      // Instantiate a thing by thing ID.
      thing = try KiiThing.loadSynchronous(withThingID: "th.rBnvSPOXBDF9r29GJeGS")
    }catch(let error as NSError){
      // Handle the error.
      return
    }
  • // Instantiate a thing by thing ID.
    KiiThing.load(withThingID: "th.rBnvSPOXBDF9r29GJeGS") { (thing , error) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
    }

Objective-C:

  • NSError* error = nil;
    
    // Instantiate a thing by thing ID.
    KiiThing* thing = [KiiThing loadSynchronousWithThingID:@"th.rBnvSPOXBDF9r29GJeGS"
                                                     error:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
  • // Instantiate a thing by thing ID.
    [KiiThing loadWithThingID:@"th.rBnvSPOXBDF9r29GJeGS"
                        block:^(KiiThing *thing, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    }];

JavaScript

// Instantiate a thing by thing ID.
KiiThing.loadWithThingID("78924c8vy2984298vvc2", {
  success: function(thing) {
    // Do something.
  },
  failure: function(error) {
    // Handle the error.
  }
});

Getting a list of things owned by the currently logged-in user

This sample code is for getting a list of things owned by the currently logged-in user or the groups of which the user is a member. You can filter the results by thing type with a method or a property, for example, the setThingType() method of the KiiThingQuery class for Java.

Android

  • // Get the current user.
    KiiUser user = KiiUser.getCurrentUser();
    
    // Create a query for things owned by the current user or the groups that the current user is a member of.
    KiiThingQuery query = new KiiThingQuery(user, user.memberOfGroups());
    
    // Query things.
    KiiThingQueryResult result = KiiThing.query(query);
    
    List<KiiThing> things = result.getResult();
    for (KiiThing thing : things) {
      // Do something.
    }
  • // Get the current user.
    KiiUser user = KiiUser.getCurrentUser();
    
    // Get a list of groups that the current user is a member of.
    user.memberOfGroups(new KiiUserCallBack() {
      @Override
      public void onMemberOfGroupsCompleted(int token, KiiUser user, List<KiiGroup> groupList, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
    
        // Create a query for things owned by the current user or the groups that the current user is a member of.
        KiiThingQuery query = new KiiThingQuery(user, groupList);
    
        // Query things.
        KiiThing.query(query, new KiiCallback<KiiThingQueryResult>() {
          @Override
          public void onComplete(KiiThingQueryResult result, Exception e) {
            List<KiiThing> things = result.getResult();
            for (KiiThing thing : things) {
              // Do something.
            }
          }
        });
      }
    });

For processing the query result, see Querying KiiObjects. Perform pagination as required.

iOS

Swift:

  • // Get the current user.
    let user : KiiUser = KiiUser.current()!
    
    do{
      // Get a list of groups that the current user is a member of.
      let groups = try user.memberOfGroupsSynchronous() as! [KiiGroup]
    
      // Create a query for things owned by the current user or the groups that the current user is a member of.
      let query = KiiThingQuery(user, groups: groups)
    
      // Query things.
      let queryResult = try KiiThing.querySynchronous(query)
      for thing in queryResult.results! {
        // Do something.
      }
    }catch(let error as NSError){
      // Handle the error.
      return
    }
  • // Get a list of groups that the current user is a member of.
    KiiUser.current()?.memberOfGroups({ (user, groups , error) in
      // Create a query for things owned by the current user or the groups that the current user is a member of.
      let query = KiiThingQuery(user, groups: (groups as! [KiiGroup]))
    
      // Query things.
      KiiThing.query(query, block: { (queryResult, query, error) in
        if error != nil {
          // Handle the error.
          return
        }
        for thing in queryResult.results! {
          // Do something.
        }
      })
    })

Objective-C:

  • NSError* error = nil;
    
    // Get the current user.
    KiiUser* user = [KiiUser currentUser];
    
    // Get a list of groups that the current user is a member of.
    NSArray* groups = [user memberOfGroupsSynchronous:&error];
    
    // Create a query for things owned by the current user or the groups that the current user is a member of.
    KiiThingQuery* query = [KiiThingQuery thingQuery:user groups:groups];
    
    // Query things.
    KiiThingQueryResult* queryResult = [KiiThing querySynchronous:query error:&error];
    
    NSArray<KiiThing * > * results = queryResult.results;
    for(KiiThing * thing in results ){
      // Do something.
    }
  • // Get the current user.
    KiiUser *user = [KiiUser currentUser];
    
    // Get a list of groups that the current user is a member of.
    [user memberOfGroupsWithBlock:^(KiiUser * _Nonnull user, NSArray * _Nullable groups, NSError * _Nullable error) {
      // Create a query for things owned by the current user or the groups that the current user is a member of.
      KiiThingQuery* query = [KiiThingQuery thingQuery:user groups:groups];
    
      // Query things.
      [KiiThing query:query block:^(KiiThingQueryResult * _Nullable queryResult, KiiThingQuery * _Nullable query, NSError * _Nullable error) {
        NSArray<KiiThing * > * results = queryResult.results;
        for(KiiThing * thing in results ){
          // Do something.
        }
      }];
    }];

JavaScript

// Get a list of groups that the current user is a member of.
KiiUser.getCurrentUser().memberOfGroups({
  success: function(user, groupList) {
    // Create a query for things owned by the current user or the groups that the current user is a member of.
    var query = KiiThingQuery.thingQuery(user, groupList);

    // Query things.
    KiiThing.executeQuery(query, {
      success: function(result) {
        var resultSet = result.getResult();
        for(var i = 0; i < resultSet.length; i++) {
          var thing = resultSet[i];
          // Do something.
        }
        if (result.hasNext()) {
          // Get the next page of the result.
          result.getNextResult({
            success: function(result) {
              // Handle the next page of the result.
              var resultSet = result.getResult();
            },
            failure: function(err) {
              // Handle the error.
            }
          });
        }
      },
      failure: function(err) {
        // Handle the error.
      }
    });
  },
  failure: function(theUser, errorString) {
    // Handle the error.
  }
});

Refreshing a thing

The following sample code refreshes a KiiThing instance with the latest thing information from the server.

A KiiThing instance created with the loadWithVendorThingID() method or the loadWithThingID() method already holds the latest information and does not need to be refreshed.

Android

  • // Instantiate a thing.
    KiiThing thing = ...
    
    try {
      // Refresh the thing.
      thing.refresh();
    } catch (AppException e) {
      // Handle the error.
    } catch (IOException e) {
      // Handle the error.
    }
  • // Instantiate a thing.
    KiiThing thing = ...
    
    // Refresh the thing.
    thing.refresh(new KiiCallback<KiiThing>() {
      @Override
      public void onComplete(KiiThing result, Exception e) {
        if (e != null) {
          // Handle the error.
          return;
        }
      }
    });

iOS

Swift:

  • // Instantiate a thing.
    let thing = ... : KiiThing
    
    do{
      // Refresh the thing.
      try thing.refreshSynchronous()
    }catch(let error as NSError){
      // Handle the error.
      return
    }
  • // Instantiate a thing.
    let thing = ... : KiiThing
    
    // Refresh the thing.
    thing.refresh() { (thing, error) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
    }

Objective-C:

  • // Instantiate a thing.
    KiiThing *thing = ...;
    
    NSError *error = nil;
    
    // Refresh the thing.
    [thing refreshSynchronous:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
  • // Instantiate a thing.
    KiiThing *thing = ...;
    
    // Refresh the thing.
    thing refresh:^(KiiThing *thing, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    }];

JavaScript

// Instantiate a thing.
var thing = ...;

// Refresh the thing.
thing.refresh({
  success: function(thing) {
    // Do something.
  },
  failure: function(error) {
    // Handle the error.
  }
});

Restoring Things (Android only)

The next sample code is restoring a thing from a Bundle and updating it with the latest information on Kii Cloud.

Android

  • try {
      // Restore a thing from a bundle.
      KiiThing thing = (KiiThing)bundle.getParcelable("my_thing");
    
      // Refresh the thing.
      thing.refresh();
    } catch (AppException e) {
      // Handle the error.
    } catch (IOException e) {
      // Handle the error.
    }
  • // Restore a thing from a bundle.
    KiiThing thing = (KiiThing)bundle.getParcelable("my_thing");
    
    // Refresh the thing.
    thing.refresh(new KiiCallback<KiiThing>() {
      @Override
      public void onComplete(KiiThing result, Exception e) {
        if (e != null) {
          // Handle the error.
          return;
        }
      }
    });

Here is the basic steps:

  1. Restore an instance of Thing from the Bundle.
  2. Execute the refresh method to get the latest thing information from Kii Cloud.