Counting KiiObjects in a Bucket

For counting all KiiObjects in a bucket, use the count(_:) method. For counting the number of KiiObjects that satisfy a specific query condition, use the count(with:_:) 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:

Swift:

  • // Prepare the target bucket to be queried.
    let bucket = Kii.bucket(withName: "people")
    
    do{
      // Count the number of the KiiObjects in the bucket.
      let count = try bucket.countObjectsSynchronous()
      print("Number of objects :\(count)")
    } catch let error as NSError {
      // Handle the error.
      return
    }
  • // Prepare the target bucket to be queried.
    let bucket = Kii.bucket(withName: "people")
    
    // Count the number of the KiiObjects in the bucket.
    bucket.count { (retBucket : KiiBucket?, retQuery : KiiQuery?, result : UInt, error : Error?) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
      // The retQuery argument has a query that returns all KiiObjects in the bucket.
      print("Number of objects :\(result)")
    }

Objective-C:

  • NSError *error = nil;
    
    // Prepare the target bucket to be queried.
    KiiBucket *bucket = [Kii bucketWithName:@"people"];
    
    // Count the number of the KiiObjects in the bucket.
    NSUInteger count = [bucket countSynchronous:&error];
    
    if (error != nil) {
      // Handle the error.
      return;
    }
    NSLog(@"Number of objects : %ld", (unsigned long)count);
  • // Prepare the target bucket to be queried.
    KiiBucket *bucket = [Kii bucketWithName:@"people"];
    
    // Count the number of the KiiObjects in the bucket.
    [bucket count:^(KiiBucket *retBucket, KiiQuery *retQuery, NSUInteger result, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
      // The retQuery argument has a query that returns all KiiObjects in the bucket.
      NSLog(@"Number of objects: %ld", (unsigned long)result);
    }];

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.

Swift:

  • // Prepare the target bucket to be queried.
    let bucket = Kii.bucket(withName: "people")
    
    // Create a query that returns KiiObjects whose "age" is equal or greater than 25.
    let clause = KiiClause.greaterThanOrEqual("age", value: NSNumber(value: 25 as Int))
    let query = KiiQuery(clause: clause)
    
    do{
      // Count the number of the KiiObjects that meet the above condition.
      let count = try bucket.countObjectsSynchronous(query, error: ())
      print("Number of objects :\(count)")
    } catch let error as NSError {
      // Handle the error.
      return
    }
  • // Prepare the target bucket to be queried.
    let bucket = Kii.bucket(withName: "people")
    
    // Create a query that returns KiiObjects whose "age" is equal or greater than 25.
    let clause = KiiClause.greaterThanOrEqual("age", value: NSNumber(value: 25 as Int))
    let query = KiiQuery(clause: clause)
    
    // Count the number of the KiiObjects that meet the above condition.
    bucket.count(with: query) { (retBucket : KiiBucket? , retQuery : KiiQuery?, result : UInt, error : Error?) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
      print("Number of objects :\(result)")
    }

Objective-C:

  • NSError *error = nil;
    
    // Prepare the target bucket to be queried.
    KiiBucket *bucket = [Kii bucketWithName:@"people"];
    
    // Create a query that returns KiiObjects whose "age" is equal or greater than 25.
    KiiClause *clause = [KiiClause greaterThanOrEqual:@"age"
                                                value:@25];
    KiiQuery *query = [KiiQuery queryWithClause:clause];
    
    // Count the number of the KiiObjects that meet the above condition.
    NSUInteger count = [bucket countSynchronousWithQuery:query
                                                andError:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    NSLog(@"Number of objects : %ld", (unsigned long)count);
  • // Prepare the target bucket to be queried.
    KiiBucket *bucket = [Kii bucketWithName:@"people"];
    
    // Create a query that returns KiiObjects whose "age" is equal or greater than 25.
    KiiClause *clause = [KiiClause greaterThanOrEqual:@"age"
                                                value:@25];
    KiiQuery *query = [KiiQuery queryWithClause:clause];
    
    // Count the number of the KiiObjects that meet the above condition.
    [bucket countWithQuery:query
                  andBlock:^(KiiBucket *retBucket, KiiQuery *retQuery, NSUInteger result, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
      NSLog(@"Number of objects: %ld", (unsigned long)result);
    }];