KiiObject のカウント

Bucket 内の全 KiiObject 数は count(_:) メソッドで取得できます。同様に、ある検索条件にヒットする KiiObject の個数は count(with:_:) メソッドで取得できます。

KiiObject 数は、実行したユーザーから実際にアクセスできる件数をカウントします。ACL によってアクセスできない KiiObject はカウント対象になりません。

KiiObject 数が多い場合、パフォーマンスが低下する場合があります。対処方法は パフォーマンス をご覧ください。

Bucket 内の全 KiiObject を数える

Bucket 内の全 KiiObject 数をカウントする例を以下に挙げます。

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);
    }];

検索条件にヒットする KiiObject を数える

検索条件にヒットする KiiObject 数をカウントする例を以下に挙げます。この例では "people" という名前の Bucket 内の KiiObject のうち、"age" が 25 以上のものをカウントしています。

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);
    }];