Creating a KiiObject

This topic explains how to create a KiiObject. For setting a JSON document in a KiiObject, see Setting a Key-value Pair.

Creating a KiiObject

In order to create a KiiObject, create a KiiObject on the client by calling the createObject() method of the destination bucket and then the save() method. Kii Cloud automatically assigns the ID of the created KiiObject.

  • let bucket = KiiUser.currentUser()!.bucketWithName("MyBucket")
    
    // Create a KiiObject.
    var obj = bucket.createObject();
    
    // Set key-value pairs.
    obj.set("score", 987);
    obj.set("mode", "easy");
    obj.set("premiumUser", false);
    
    // Save the KiiObject.
    obj.save().then(
      function(theObject) {
        // Do something.
      }
    ).catch(
      function(error) {
        var theObject = error.target;
        var errorString = error.message;
        // Handle the error.
      }
    );
  • let bucket = KiiUser.currentUser()!.bucketWithName("MyBucket")
    
    // Create a KiiObject.
    var obj = bucket.createObject();
    
    // Set key-value pairs.
    obj.set("score", 987);
    obj.set("mode", "easy");
    obj.set("premiumUser", false);
    
    // Save the KiiObject.
    obj.save({
      success: function(theObject) {
        // Do something.
      },
      failure: function(theObject, errorString) {
        // Handle the error.
      }
    });

Make sure to call the save() method. The key-value pairs set in the KiiObject are not saved on Kii Cloud until the method is called.

The code above creates a KiiObject in the specified bucket, MyBucket in the user scope in this sample, on Kii Cloud.

In the code above, the set() method is called to set values in three fields before the save() method. For procedures for setting values, see Setting a Key-value Pair.

Creating a KiiObject with an ID

Kii Cloud automatically assigns an ID to a newly created KiiObject. You can also choose to have your mobile app to assign an ID to a KiiObject.

The following code creates a KiiObject with the same parameters as those in the previous example except that the ID is explicitly specified.

  • var id = "score_userX";
    
    // Check whether the KiiObject ID is valid.
    if(!KiiObject.isValidObjectID(id)) {
      // The KiiObject ID is invalid.
    }
    
    // Create a KiiObject with the specific ID.
    var obj = KiiUser.currentUser()!.bucketWithName("MyBucket").createObjectWithID(id);
    
    // Set key-value pairs.
    obj.set("score", 987);
    obj.set("mode", "easy");
    obj.set("premiumUser", false);
    
    // Save the KiiObject.
    obj.saveAllFields().then(
      function(theObject) {
        // Do something.
      }
    ).catch(
      function(error) {
        var theObject = error.target;
        var errorString = error.message;
        // Handle the error.
      }
    );
  • var id = "score_userX";
    
    // Check whether the KiiObject ID is valid.
    if(!KiiObject.isValidObjectID(id)) {
      // The KiiObject ID is invalid.
    }
    
    // Create a KiiObject with the specific ID.
    var obj = KiiUser.currentUser()!.bucketWithName("MyBucket").createObjectWithID(id);
    
    // Set key-value pairs.
    obj.set("score", 987);
    obj.set("mode", "easy");
    obj.set("premiumUser", false);
    
    // Save the KiiObject.
    obj.saveAllFields({
      success: function(theObject) {
        // Do something.
      },
      failure: function(theObject, errorString) {
        // Handle the error.
      }
    });

You specify an ID when executing the createObjectWithID() method. You can check the validity of the ID with the isValidObjectID() method (For the valid ID pattern, see the JSDoc).

Call the saveAllFields() method to create a new KiiObject with the specified ID on Kii Cloud. You cannot use the save() method when you create a KiiObject with a specific ID.

If the KiiObject with the ID "score_userX" already exists on the server, the above sample code will overwrite the existing KiiObject. You can prevent this overwriting by setting the overwrite argument of the saveAllFields() method to false. See the JSDoc for more details.

Tips for accessing KiiObjects

  • Cannot write in a bucket in the application scope

    All users, including anonymous users who have not logged in yet, can read the application scope. Writing in the application scope, however, requires the users to log in. If you want to avoid forcing your application users to log in, we recommend you to leverage the "pseudo user" feature (and not to change the ACL of the application scope). See Pseudo Users for more discussion.

    You need to be careful about the security when you are using the application scope. See Security for more details.

  • Cannot read a KiiObject that has been written in a bucket

    Double check if the scope of the bucket is correct.

    Remember that buckets in different scopes are separate even if they have the same name. The same applies when you use the data browser in the developer portal.

    For example, KiiObjects created in the application scope with Kii.bucketWithName("myBucket") are not readable with KiiUser.getCurrentUser().bucketWithName("myBucket") because this method looks for the bucket in the user scope.