部分アップデート(更新チェックなし)
クライアントから送信されたキーと値のペアと、サーバー上の既存のキーと値のペアをサーバー上でマージして更新する方法です。変更の差分だけをキーと値のペアとしてクライアント側で用意して、サーバーに送信します。なお、サーバー側のキーそのものはこの方法で削除できません。
更新の際、サーバー上で他のクライアントから更新があったかどうかはチェックせずにそのまま上書きします。
更新のコード例を以下に挙げます。
Swift:
-
// Instantiate a KiiObject. let object = KiiObject(uri: "Set the URI of an existing KiiObject here")! // 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") do{ // Save and partially update the KiiObject. // This method appends the key-value pairs generated locally // to the KiiObject on the server. try object.saveSynchronous() } catch let error as NSError { // Handle the error. return }
-
// Instantiate a KiiObject. let object = KiiObject(uri: "Set the URI of an existing KiiObject here")! // 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") // Save and partially update the KiiObject. // This method appends the key-value pairs generated locally // to the KiiObject on the server. object.save { (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"]; // 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"]; NSError *error = nil; // Save and partially update the KiiObject. // This method appends the key-value pairs generated locally // to the KiiObject on the server. [object saveSynchronous:&error]; if (error != nil) { // Handle the error. return; }
-
// Instantiate a KiiObject. KiiObject *object = [KiiObject objectWithURI:@"Set the URI of an existing KiiObject here"]; // 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"]; // Save and partially update the KiiObject. // This method appends the key-value pairs generated locally // to the KiiObject on the server. [object saveWithBlock:^(KiiObject *object, NSError *error) { if (error != nil) { // Handle the error. return; } }];
ここでは以下の処理を行っています。
- 更新対象の KiiObject を用意します。ここでは、URI からの生成によって、既存の KiiObject を作成しています。コード上の URI は、事前に取得しておいたものに置き換えてください。
- 更新内容の差分をキーと値のペアとして設定します。
save(_:)
メソッドにより更新します。上記のコードでは、更新チェックを行わないため、そのまま強制的に上書きします。
部分アップデートでは、通常 refresh は不要です。事前に refresh(_:)
メソッドを実行すると、クライアント側にはサーバーのすべてのキーと値のペアが取得されるため、フルアップデートと同様の結果になります。
部分アップデートで remove(_:forKey:)
メソッドを使ってキーと値のペアを削除しても、サーバー側とのマージが行われるため、結果としてそのキーは消えません。キーと値のペアを消すには、フルアップデートを実行してください。