フルアップデート(更新チェックなし)
クライアントから送信されたキーと値のペアで、サーバー上のデータを完全に上書きする方法です。サーバーにあった値は、上書きによって失われます。
更新の際、サーバー上で他のクライアントから更新があったかどうかはチェックせずにそのまま上書きします。
更新のコード例を以下に挙げます。
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") 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(true) } 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") 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(true, 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"]; // 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"]; NSError *error = nil; // 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:YES withError:&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"]; [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:YES withBlock:^(KiiObject *object, NSError *error) { if (error != nil) { // Handle the error. return; } }];
ここでは以下の処理を行っています。
- 更新対象の KiiObject を用意します。ここでは、URI からの生成によって、既存の KiiObject を作成しています。コード上の URI は、事前に取得しておいたものに置き換えてください。
setObject(_:forKey:)
メソッドでキーと値のペアの追加を、remove(forKey_:)
メソッドでキーと値のペアの削除を行います。更新後はここでobject
に設定されている値だけになります。必要に応じて、事前にrefresh(_:)
メソッドを呼び出して、すべてのキーと値のペアをサーバーから取得しておくこともできます(実装方法は KiiObject の取得 を参照してください)。saveAllFields(_:with:_:)
メソッドにより更新します。上記のコードでは、引数値 true によって更新チェックを行わないため、そのまま強制的に上書きします。