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

You can get a KiiObject ID by executing the getID() method.

// Get the ID of an existing KiiObject.
var id = object.getID();

Referencing a KiiObject by ID

First, execute the createObjectWithID() 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.
    var object = Kii.bucketWithName("_app_bucket_").createObjectWithID(id);
    
    // Refresh the KiiObject to retrieve the latest data from Kii Cloud.
    object.refresh().then(
      function(theObject) {
        // Do something.
      }
    ).catch(
      function(error) {
        var theObject = error.target;
        var errorString = error.message;
        // Handle the error.
      }
    );
  • // Instantiate a KiiObject in a bucket "_app_bucket_" in the application scope.
    var object = Kii.bucketWithName("_app_bucket_").createObjectWithID(id);
    
    // Refresh the KiiObject to retrieve the latest data from Kii Cloud.
    object.refresh({
      success: function(theObject) {
        // Do something.
      },
      failure: function(theObject, errorString) {
        // Handle the error.
      }
    });

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 objectURI() method of the target KiiObject.

// Get the URL of an existing KiiObject.
var uri = object.objectURI();

Referencing a KiiObject by URI

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

  • // Instantiate a KiiObject.
    var object = KiiObject.objectWithURI(uri);
    
    // Refresh the KiiObject to retrieve the latest data from Kii Cloud.
    object.refresh().then(
      function(theObject) {
        // Do something.
      }
    ).catch(
      function(error) {
        var theObject = error.target;
        var errorString = error.message;
        // Handle the error.
      }
    );
  • // Instantiate a KiiObject.
    var object = KiiObject.objectWithURI(uri);
    
    // Refresh the KiiObject to retrieve the latest data from Kii Cloud.
    object.refresh({
      success: function(theObject) {
        // Do something.
      },
      failure: function(theObject, errorString) {
        // Handle the error.
      }
    });

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 objectWithURI() 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.