Counting KiiObjects in a Bucket

For counting all KiiObjects in a bucket, use the count() or count(CountCallBack) method. For counting the number of KiiObjects that satisfy a specific query condition, use the count(KiiQuery) or count(KiiQuery, CountCallBack) method.

These methods count KiiObjects that are accessible to the current user. Those that are not accessible because of the ACL setting are not counted.

The performance of the count operation can deteriorate if there are numerous KiiObjects. See Performance for more information.

Counting all KiiObjects in a bucket

You can get the number of KiiObjects in a bucket with the following sample code:

  • // Prepare the target bucket to be queried.
    KiiBucket bucket = Kii.bucket("people");
    
    try {
      // Count the number of the KiiObjects in the bucket.
      int count = bucket.count();
      Log.v("CountObjects", "Number of objects : " + count);
    } catch (IOException e) {
      // Handle the error.
    } catch (AppException e) {
      // Handle the error.
    }
  • // Prepare the target bucket to be queried.
    KiiBucket bucket = Kii.bucket("people");
    
    // Count the number of the KiiObjects in the bucket.
    bucket.count(new CountCallBack() {
      @Override
      public void onCountCompleted(KiiBucket bucket, KiiQuery query, int count, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
        Log.v("CountObjects", "Number of objects : " + count);
        // The query argument has a query that returns all KiiObjects in the bucket.
      }
    });

Counting KiiObjects in a bucket with a query condition

You can get the number of KiiObjects that satisfy a query condition in a bucket with the following sample code. Suppose you have a bucket named "people" and want to know the number of KiiObjects that have a value of 25 or higher for the "age" field.

  • // Prepare the target bucket to be queried.
    KiiBucket bucket = Kii.bucket("people");
    
    // Create a query that returns KiiObjects whose "age" is equal or greater than 25.
    KiiClause clause = KiiClause.greaterThanOrEqual("age", 25);
    KiiQuery query = new KiiQuery(clause);
    
    try {
      // Count the number of the KiiObjects that meet the above condition.
      int count = bucket.count(query);
      Log.v("CountObjects", "Number of objects : " + count);
    } catch (IOException e) {
      // Handle the error.
    } catch (AppException e) {
      // Handle the error.
    }
  • // Prepare the target bucket to be queried.
    KiiBucket bucket = Kii.bucket("people");
    
    // Create a query that returns KiiObjects whose "age" is equal or greater than 25.
    KiiClause clause = KiiClause.greaterThanOrEqual("age", 25);
    KiiQuery query = new KiiQuery(clause);
    
    // Count the number of the KiiObjects that meet the above condition.
    bucket.count(query, new CountCallBack() {
      @Override
      public void onCountCompleted(KiiBucket bucket, KiiQuery query, int count, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
        Log.v("CountObjects", "Number of objects : " + count);
      }
    });