Full Update with the Overwrite Check

This method overwrites the data on the server with the key-value pairs sent from the client (the data on the server will be lost).

This method checks for overwrites using a technique known as optimistic locking. If the data on the server has been updated by other clients after you got the KiiObject, your update is rejected with an error.

Here is the sample code:

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;
        }
      }];
    }];

This is what is happening in the sample code:

  1. Prepare a KiiObject to update. In this sample code, we are creating a KiiObject with its URI (Replace the URI in the code with the real one when you are running the code).
  2. Use the refresh(_:) method to get the latest key-value pairs in the server. This is required to get the latest _version value that indicates the update status of the KiiObject.
  3. Add key-value pairs with the setObject(_:forKey:) method and remove a key-value pair with the remove(forKey:) method. Only the key-value pairs set here will be in the KiiOject after the updating.
  4. Execute the saveAllFields(_:with:_:) method to update the KiiObject. By setting the first argument to false, we are enabling the overwrite check; the method returns an error if the KiiObject on the server has been updated by another client.