グループの参照
モバイルアプリからグループ内のデータを参照するには、以下の方法があります。
- URI によるグループの参照
- ID によるグループの参照
- メンバーやオーナーによるグループの参照
このページでは、URI や ID を使ったグループの参照方法を説明します。3 つ目に示した、メンバーやオーナーとなっているグループを参照する方法は、ユーザーとグループの関連 で説明します。
グループの作成時に指定したグループ名では、既存グループを参照できません。参照しようとすると、同名の新しいグループが作成されます。
ここでは、URI からグループを参照する例を示します。ID を使う場合も同様に実装できます。
URI からグループを参照するには、グループの作成直後にそのグループの URI を取得し、URI をどこかに保存しておく必要があります。
URI の取得と保存
グループを作成した際、ログイン中のユーザーから参照できる場所に URI を保存しておきます。
この例では、ユーザースコープの
groupChatBucket
にある KiiObject に、ChatGroupURI
というキーで URI の文字列を保存しています。Bucket を使わず、OS 依存のストレージ(Android の SharedPreference や、iOS の NSUserDefaults など)に保存することもできます。URI の取得
グループを参照する際には、
ChatGroupURI
から URI を取得します。URI によるグループの参照
URI から KiiGroup インスタンスを生成します。これにより、元の KiiGroup と同じグループをクライアント上に復元できます。
リフレッシュ
グループのメンバーなどの情報にアクセスするためには、KiiGroup をリフレッシュして、グループのコンテンツを読み込みます。詳細は オブジェクトの ID と URI を参照してください。
なお、グループスコープのデータにアクセスするなど、グループの URI や ID が特定できていれば十分な場合、リフレッシュは不要です。
URI によるグループの参照
URI によってグループを再インスタンス化する例を以下に挙げます。
-
// ... When the group is created ... // Get the reference URI of the existing group. Uri groupUri = group.toUri(); // ... When you need to access the group ... // Instantiate the group again. KiiGroup group2 = KiiGroup.createByUri(groupUri); try { // Refresh the group to retrieve the latest data from Kii Cloud. group2.refresh(); // Do something. } catch (GroupOperationException e) { // Handle the error. }
-
// ... When the group is created ... // Get the reference URI of the existing group. Uri groupUri = group.toUri(); // ... When you need to access the group ... // Instantiate the group again. KiiGroup group2 = KiiGroup.createByUri(groupUri); // Refresh the group to retrieve the latest data from Kii Cloud. group2.refresh(new KiiGroupCallBack() { @Override public void onRefreshCompleted(int token, KiiGroup group2, Exception exception) { if (exception != null) { // Handle the error. return; } // Do something. } });
URI を使って既存グループをインスタンス化する場合は createByUri()
メソッドを実行します。この際、グループを最新のものに更新するために refresh()
メソッドを実行してください。
IDによるグループの参照
ID によってグループを再インスタンス化する例を以下に挙げます。
-
// ... When the group is created ... // Get the ID of the existing group. String groupID = group.getID(); // ... When you need to access the group ... // Instantiate the group again. KiiGroup group2 = KiiGroup.groupWithID(groupID); try { // Refresh the group to retrieve the latest data from Kii Cloud. group2.refresh(); // Do something. } catch (GroupOperationException e) { // Handle the error. }
-
// ... When the group is created ... // Get the ID of the existing group. String groupID = group.getID(); // ... 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.refresh(new KiiGroupCallBack() { @Override public void onRefreshCompleted(int token, KiiGroup group2, Exception exception) { if (exception != null) { // Handle the error. return; } // Do something. } });
ID を使って既存グループをインスタンス化する場合は groupWithID()
メソッドを実行します。この際、グループを最新のものに更新するために refresh()
メソッドを実行してください。