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.
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.
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.