Getting a KiiObject

There are two methods to get KiiObjects on Kii Cloud: getting a specific KiiObject by ID or URI and getting a bunch of KiiObjects that meet a condition within a bucket by querying KiiObjects. This topic explains how to get a KiiObject by ID or URI.

Either method allows you to get data such as key-value pairs in a KiiObject. See Getting a Key-value Pair for getting key-value pairs from an obtained KiiObject.

In order to get a KiiObject by ID or URI, you need to save the ID or URI of a KiiObject somewhere when it is created. You can get the KiiObject with the saved ID or URI later.

Using a KiiObject ID

Getting a KiiObject ID

To get a KiiObject ID, execute the toURI() method of the target KiiObject to get its URI first. Then, extract the ID from the URI.

// Get the ID of an existing KiiObject.
String uri = object.toUri().toString();
String id = uri.substring(uri.lastIndexOf('/') + 1);

Referencing a KiiObject by ID

First, execute the object() method of the bucket where the KiiObject is stored with the ID. This will create an instance of the KiiObject.

  • // Instantiate a KiiObject in a bucket "_app_bucket_" in the application scope.
    KiiObject object = Kii.bucket("_app_bucket_").object(id);
    
    try {
      // Refresh the KiiObject to retrieve the latest data from Kii Cloud.
      object.refresh();
    } catch (IOException e) {
      // Handle the error.
    } catch (AppException e) {
      // Handle the error.
    }
  • // Instantiate a KiiObject in a bucket "_app_bucket_" in the application scope.
    KiiObject object = Kii.bucket("_app_bucket_").object(id);
    
    // Refresh the KiiObject to retrieve the latest data from Kii Cloud.
    object.refresh(new KiiObjectCallBack() {
      @Override
      public void onRefreshCompleted(int token, KiiObject object, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
      }
    });

The KiiObject ID is unique only within a bucket. Therefore, when instantiating a KiiObject, specify the target bucket explicitly as shown in the sample code above. Note that there could be multiple KiiObjects with the same ID in other buckets. See Object ID and URI for more discussion.

After a KiiObject is instantiated, execute the refresh() method and then access the key-value pairs in the KiiObject. By executing the refresh() method, the latest KiiObject data is fetched from Kii Cloud and the local KiiObject is refreshed with the latest data. You need to refresh the KiiObject instance explicitly, or it will not be updated with the latest data.

Using a KiiObject URI

Getting a KiiObject URI

To get a KiiObject URI, execute the toURI() method of the target KiiObject.

// Get the URL of an existing KiiObject.
Uri uri = object.toUri();

Referencing a KiiObject by URI

First, execute the createByUri() method with the URI. This will create an instance of the KiiObject.

  • // Instantiate a KiiObject.
    KiiObject object = KiiObject.createByUri(uri);
    
    try {
      // Refresh the KiiObject to retrieve the latest data from Kii Cloud.
      object.refresh();
    } catch (IOException e) {
      // Handle the error.
    } catch (AppException e) {
      // Handle the error.
    }
  • // Instantiate a KiiObject.
    KiiObject object = KiiObject.createByUri(uri);
    
    // Refresh the KiiObject to retrieve the latest data from Kii Cloud.
    object.refresh(new KiiObjectCallBack() {
      @Override
      public void onRefreshCompleted(int token, KiiObject object, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
      }
    });

The KiiObject URI contains the information about the bucket that the KiiObject belongs to. Therefore, you do not need to specify the target bucket explicitly as shown in the sample code above. See Object ID and URI for more discussion.

After a KiiObject is instantiated, execute the refresh() method and then access the key-value pairs in the KiiObject. By executing the refresh() method, the latest KiiObject data is fetched from Kii Cloud and the local KiiObject is refreshed with the latest data. You need to refresh the KiiObject instance, or it will not be updated with the latest data.

Note that a KiiObject that is instantiated with the createByUri() method does not have its ID locally until the refresh() method is executed. Make sure to execute the refresh() method before getting the KiiObject ID.