Retrieving a Group

You can use the following methods to retrieve data of a group from your mobile app:

This topic explains how to retrieve a group by URI and ID. For the third method, retrieving a group for its member or owner, see Relationship between a User and a Group.

You cannot reference an existing group by its name that was specified when the group was created. If you attempt to do so, a new group with the same name will be created.

This topic explains how to reference a group by its URI. You can also use the group ID in a similar way.

In order to reference a group by its URI, get and save it somewhere just after the group is created.

  1. Get and save the URI

    When a group is created, save its URI in the location where the logged-in user can access.

    In this example, the URI string is saved with the ChatGroupURI key in a KiiObject in the groupChatBucket bucket in the user scope. Instead of a bucket, you can also use OS-dependent storage, such as the shared preferences of Android and NSUserDefaults of iOS.

  2. Retrieve the URI

    Retrieve the URI with the ChatGroupURI key for referencing the group.

  3. Reference the group by the URI

    Instantiate the KiiGroup object by its URI. The group in the original state can be restored on the client.

  4. Refresh the group

    In order to access information such as the group members, refresh the KiiGroup instance to load the group content. For more information, see Object ID and URI.

    You do not need to refresh the group if you need only the group URI or ID, for example, to access data in the group scope.

Retrieving a group by URI

The following sample code shows how to reinstantiate a group with its URI.

Swift 3:

  • // ... When the group is created ...
    
    // Get the reference URI of the existing group.
    let groupUri = group.objectURI!
    
    // ... When you need to access the group ...
    
    // Instantiate the group again.
    let group2 = KiiGroup(uri: groupUri)
    
    do{
      // Refresh the group to retrieve the latest data from Kii Cloud.
      try group2.refreshSynchronous()
    } catch let error as NSError {
      // Handle the error.
      return
    }
    
    // Do something.
  • // ... When the group is created ...
    
    // Get the reference URI of the existing group.
    let groupUri = group.objectURI!
    
    // ... When you need to access the group ...
    
    // Instantiate the group again.
    let group2 = KiiGroup(uri: groupUri)
    
    // Refresh the group to retrieve the latest data from Kii Cloud.
    group2.refresh{ (group2 : KiiGroup?, error : Error?) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
      // Do something.
    }

Objective-C:

  • // ... When the group is created ...
    
    // Get the reference URI of the existing group.
    NSString *groupUri = [group objectURI];
    
    // ... When you need to access the group ...
    
    NSError *error;
    
    // Instantiate the group again.
    KiiGroup *group2 = [KiiGroup groupWithURI:groupUri];
    
    // Refresh the group to retrieve the latest data from Kii Cloud.
    [group2 refreshSynchronous:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    // Do something.
     
  • // ... When the group is created ...
    
    // Get the reference URI of the existing group.
    NSString *groupUri = [group objectURI];
    
    // ... When you need to access the group ...
    
    // Instantiate the group again.
    KiiGroup *group2 = [KiiGroup groupWithURI:groupUri];
    
    // Refresh the group to retrieve the latest data from Kii Cloud.
    [group2 refreshWithBlock:^(KiiGroup *group2, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
      // Do something.
    }];

As you can see in the sample code, you can instantiate the already-existing group by executing the KiiGroup(uri:) method. You will need to execute the refresh(_:) method after the instantiating the group to make it up-to-date.

Retrieving a group by ID

The following sample code shows how to reinstantiate a group with its ID.

Swift 3:

  • // ... When the group is created ...
    
    // Get the ID of the existing group.
    let groupID = group.groupID!
    
    // ... When you need to access the group ...
    
    // Instantiate the group again.
    let group2 = KiiGroup(id: groupID)
    
    do{
      // Refresh the group to retrieve the latest data from Kii Cloud.
      try group2.refreshSynchronous()
    } catch let error as NSError {
      // Handle the error.
      return
    }
    
    // Do something.
  • // ... When the group is created ...
    
    // Get the ID of the existing group.
    let groupID = group.groupID!
    
    // ... When you need to access the group ...
    
    // Instantiate the group again.
    let group2 = KiiGroup(id: groupID)
    
    // Refresh the group to retrieve the latest data from Kii Cloud.
    group2.refresh{ (group2 : KiiGroup?, error : Error?) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
      // Do something.
    }

Objective-C:

  • // ... When the group is created ...
    
    // Get the ID of the existing group.
    NSString *groupID = [group groupID];
    
    // ... When you need to access the group ...
    
    NSError *error;
    
    // Instantiate the group again.
    KiiGroup *group2 = [KiiGroup groupWithID:groupID];
    
    // Refresh the group to retrieve the latest data from Kii Cloud.
    [group2 refreshSynchronous:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    // Do something.
     
  • // ... When the group is created ...
    
    // Get the ID of the existing group.
    NSString *groupID = [group groupID];
    
    // ... When you need to access the group ...
    
    // Instantiate the group again.
    KiiGroup *group2 = [KiiGroup groupWithID:groupID];
    
    // Refresh the group to retrieve the latest data from Kii Cloud.
    [group2 refreshWithBlock:^(KiiGroup *group2, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
      // Do something.
    }];