Custom Fields

You can add some custom fields (e.g. "age", "gender" and "birthday") too. You can use any field names as long as they are not predefined by the SDK. To learn more about the predefined fields, please read the Javadoc.

You will set your custom fields in the UserFields instance.

Setting and updating custom fields

The following example illustrates how to set the custom fields.

  • try {
      // Set custom fields.
      UserFields userFields = new UserFields();
      userFields.set("age", 20);
      userFields.set("gender", "male");
      userFields.set("height", 170.5);
      userFields.set("isMember", true);
    
      // Remove custom fields.
      userFields.removeFromServer("weight");
      userFields.removeFromServer("chest");
    
      // Get the currently logged-in user.
      KiiUser user = KiiUser.getCurrentUser();
    
      // Update the user attributes.
      user.update(null, userFields);
    } catch (IOException e) {
      // Handle the error.
    } catch (AppException e) {
      // Handle the error.
    }
  • // Set custom fields.
    UserFields userFields = new UserFields();
    userFields.set("age", 20);
    userFields.set("gender", "male");
    userFields.set("height", 170.5);
    userFields.set("isMember", true);
    
    // Remove custom fields.
    userFields.removeFromServer("weight");
    userFields.removeFromServer("chest");
    
    // Get the currently logged-in user.
    KiiUser user = KiiUser.getCurrentUser();
    
    // Update the user attributes.
    user.update(null, userFields, new KiiUserUpdateCallback() {
      @Override
      public void onUpdateCompleted(KiiUser kiiUser, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
      }
    });

If an existing field is not explicitly specified for the update operation, the field remains unchanged on Kii Cloud.

The fields are updated on Kii Cloud and in the KiiUser instance after the update method is successfully completed.

You can update predefiend and custom fields at the same time.

Only users themselves can update their fields. See User Attributes to learn more on who can access the user's fields.

Note: The KiiUser#update() and KiiUser#update(KiiUserCallBack) methods are now deprecated. Please use the KiiUser#update(IdentityData, UserFields) or KiiUser#update(IdentityData, UserFields, KiiUserUpdateCallback) methods instead.

Getting custom fields

The following example illustrates how to get the custom fields. You need to call the getter methods that correspond to the target values. To learn more, please read the Javadoc.

  • // Get the currently logged-in user.
    KiiUser user = KiiUser.getCurrentUser();
    
    try {
      // Refresh the user to get the latest user info from Kii Cloud.
      user.refresh();
    
      // Get custom fields.
      int age = user.getInt("age");
      String gender = user.getString("gender");
      double height = user.getDouble("height");
      boolean isMember = user.getBoolean("isMember");
    } catch (IOException e) {
      // Handle the error.
    } catch (AppException e) {
      // Handle the error.
    }
  • // Get the currently logged-in user.
    KiiUser user = KiiUser.getCurrentUser();
    
    // Refresh the user to get the latest user info from Kii Cloud.
    user.refresh(new KiiUserCallBack() {
      @Override
      public void onRefreshCompleted(int token, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
    
        // Get custom fields.
        int age = user.getInt("age");
        String gender = user.getString("gender");
        double height = user.getDouble("height");
        boolean isMember = user.getBoolean("isMember");
      }
    });

To get a list of available key-value pairs, call the keySet method like the following example:

  • // Get the currently logged-in user.
    KiiUser user = KiiUser.getCurrentUser();
    
    try {
      // Refresh the user to get the latest user info from Kii Cloud.
      user.refresh();
    
      // Get a set of custom field keys.
      HashSet<String> keyset = user.keySet();
    } catch (IOException e) {
      // Handle the error.
    } catch (AppException e) {
      // Handle the error.
    }
  • // Get the currently logged-in user.
    KiiUser user = KiiUser.getCurrentUser();
    
    // Refresh the user to get the latest user info from Kii Cloud.
    user.refresh(new KiiUserCallBack() {
      @Override
      public void onRefreshCompleted(int token, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
    
        // Get a set of custom field keys.
        HashSet<String> keyset = user.keySet();
      }
    });

Setting custom fields upon the user registration

The following example illustrates how to set the custom fields upon the user registration.

As shown in the sample code, you can set the custom fields with the set method after creating a KiiUser instance.

  • // Create a user.
    KiiUser.Builder builder = KiiUser.builderWithName("user_123456");
    builder.setEmail("user_123456@example.com");
    builder.setGlobalPhone("+819012345678");
    KiiUser user = builder.build();
    
    // Set custom fields.
    user.set("age", 30);
    user.set("score", 0);
    
    try {
      // Register the user.
      user.register("123ABC");
    } catch (AppException e) {
      // Handle the error.
    } catch (IOException e) {
      // Handle the error.
    }
  • // Create a user.
    KiiUser.Builder builder = KiiUser.builderWithName("user_123456");
    builder.setEmail("user_123456@example.com");
    builder.setGlobalPhone("+819012345678");
    KiiUser user = builder.build();
    
    // Set custom fields.
    user.set("age", 30);
    user.set("score", 0);
    
    // Register the user.
    user.register(new KiiUserCallBack() {
      @Override
      public void onRegisterCompleted(int token, KiiUser user, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
      }
    }, "123ABC");