グループの参照
モバイルアプリからグループ内のデータを参照するには、以下の方法があります。
- 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 によってグループを再インスタンス化する例を以下に挙げます。
Swift:
-
// ... 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. }];
URI を使って既存グループをインスタンス化する場合は KiiGroup(uri:)
メソッドを実行します。この際、グループを最新のものに更新するために refresh(_:)
メソッドを実行してください。
IDによるグループの参照
ID によってグループを再インスタンス化する例を以下に挙げます。
Swift:
-
// ... 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. }];