フルアップデート(更新チェックあり)
クライアントから送信されたキーと値のペアで、サーバー上のデータを完全に上書きする方法です。サーバーにあった値は、上書きによって失われます。
更新の際、「楽観的ロック(Optimistic lock)」の機能によるチェックを行います。クライアントへの KiiObject の取得以降に、サーバー上で他のクライアントからの更新があった場合は、更新処理がエラー応答します。
更新のコード例を以下に挙げます。
Swift:
-
// Instantiate a KiiObject. let object = KiiObject(uri: "Set the URI of an existing KiiObject here")! do{ // Refresh the KiiObject to get the latest key-value pairs. try object.refreshSynchronous() } catch let error as NSError { // Handle the error. return } // Update key-value pairs. object.setObject(NSNumber(value: 1 as Int), forKey: "myid") object.setObject("John Doe Jr", forKey: "name") object.setObject("john_jr@example.com", forKey: "email") object.remove(forKey: "address") do{ // 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. try object.saveAllFieldsSynchronous(false) } catch let error as NSError { // Handle the error. return }
-
// Instantiate a KiiObject. let object = KiiObject(uri: "Set the URI of an existing KiiObject here")! // Refresh the KiiObject to get the latest key-value pairs. object.refresh { (object : KiiObject?, error : Error?) -> Void in if error != nil { // Handle the error. return } // Update key-value pairs. object!.setObject(NSNumber(value: 1 as Int), forKey: "myid") object!.setObject("John Doe Jr", forKey: "name") object!.setObject("john_jr@example.com", forKey: "email") object!.remove(forKey: "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, with: { (object : KiiObject?, error : Error?) -> Void in if error != nil { // Handle the error. return } }) }
Objective-C:
-
// Instantiate a KiiObject. KiiObject *object = [KiiObject objectWithURI:@"Set the URI of an existing KiiObject here"]; NSError *error = nil; // Refresh the KiiObject to get the latest key-value pairs. [object refreshSynchronous:&error]; if (error != nil) { // Handle the error. return; } // Update key-value pairs. [object setObject:[NSNumber numberWithInt:1] forKey:@"myid"]; [object setObject:@"John Doe Jr" forKey:@"name"]; [object setObject:@"john_jr@example.com" forKey:@"email"]; [object removeObjectForKey:@"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 saveAllFieldsSynchronous:NO withError:&error]; if (error != nil) { // Handle the error. return; }
-
// Instantiate a KiiObject. KiiObject *object = [KiiObject objectWithURI:@"Set the URI of an existing KiiObject here"]; // Refresh the KiiObject to get the latest key-value pairs. [object refreshWithBlock:^(KiiObject *object, NSError *error) { if (error != nil) { // Handle the error. return; } // Update key-value pairs. [object setObject:[NSNumber numberWithInt:1] forKey:@"myid"]; [object setObject:@"John Doe Jr" forKey:@"name"]; [object setObject:@"john_jr@example.com" forKey:@"email"]; [object removeObjectForKey:@"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:NO withBlock:^(KiiObject *object, NSError *error) { if (error != nil) { // Handle the error. return; } }]; }];
ここでは以下の処理を行っています。
- 更新対象の KiiObject を用意します。ここでは、URI からの生成によって、既存の KiiObject を作成しています。コード上の URI は、事前に取得しておいたものに置き換えてください。
refresh(_:)
メソッドによってサーバーの KiiObject を取得します。更新チェックを行う場合は KiiObject の更新状態を示す_version
値が必要なため、取得処理は必須です。setObject(_:forKey:)
メソッドでキーと値のペアの追加を、remove(forKey:)
メソッドでキーと値のペアの削除を行います。更新後はここでobject
に設定されている値だけになります。saveAllFields(_:with:_:)
メソッドの第 1 引数を false にして更新します。サーバー側の KiiObject が他のクライアントによって書き換えられていた場合は、API がエラー応答します。