フルアップデート(更新チェックあり)
クライアントから送信されたキーと値のペアで、サーバー上のデータを完全に上書きする方法です。サーバーにあった値は、上書きによって失われます。
更新の際、「楽観的ロック(Optimistic lock)」の機能によるチェックを行います。クライアントへの KiiObject の取得以降に、サーバー上で他のクライアントからの更新があった場合は、更新処理がエラー応答します。
更新のコード例を以下に挙げます。
-
// Instantiate a KiiObject. Uri objUri = Uri.parse("Set the URI of an existing KiiObject here"); KiiObject object = KiiObject.createByUri(objUri); try { // Refresh the KiiObject to get the latest key-value pairs. object.refresh(); // Update key-value pairs. object.set("myid", 1); object.set("name", "John Doe Jr"); object.set("email", "john_jr@example.com"); object.remove("address"); // Save and fully update the KiiObject. // This method removes all key-value pairs from the KiiObject on the server and // adds the key-value pairs generated locally to the KiiObject. object.saveAllFields(false); } catch (AppException e) { // Handle the error. } catch (IOException e) { // Handle the error. }
-
// Instantiate a KiiObject. Uri objUri = Uri.parse("Set the URI of an existing KiiObject here"); KiiObject object = KiiObject.createByUri(objUri); // Refresh the KiiObject to get the latest key-value pairs. object.refresh(new KiiObjectCallBack() { @Override public void onRefreshCompleted(int token, KiiObject object, Exception exception) { if (exception != null) { // Handle the error. return; } // Update key-value pairs. object.set("myid", 1); object.set("name", "John Doe Jr"); object.set("email", "john_jr@example.com"); object.remove("address"); // Save and fully update the KiiObject. // This method removes all key-value pairs from the KiiObject on the server and // adds the key-value pairs generated locally to the KiiObject. object.saveAllFields(new KiiObjectCallBack() { @Override public void onSaveCompleted(int token, KiiObject object, Exception exception) { if (exception != null) { // Handle the error. return; } } }, false); } });
ここでは以下の処理を行っています。
- 更新対象の KiiObject を用意します。ここでは、URI からの生成によって、既存の KiiObject を作成しています。コード上の URI は、事前に取得しておいたものに置き換えてください。
refresh()
メソッドによってサーバーの KiiObject を取得します。更新チェックを行う場合は KiiObject の更新状態を示す_version
値が必要なため、取得処理は必須です。set()
メソッドでキーと値のペアの追加を、remove()
メソッドでキーと値のペアの削除を行います。更新後はここでobject
に設定されている値だけになります。saveAllFields()
メソッドの引数を false にして更新します。サーバー側の KiiObject が他のクライアントによって書き換えられていた場合は、API がエラー応答します。