Backendless Support
 

Updating data objects using API

Once an object is stored in the Backendless data storage, any of its properties (except for the system ones) can be updated using the data update API. 

The API works the same way as the initial call for saving the object. To update a property value, simply modify it in the instance representing the saved object. The example below retrieves a saved object, modifies the "name" property and saves it back in the data store.

The data table and schema look as shown in the screenshots below:

Schema:

 Person class:

package com.mbaas.sample;
import java.util.Date;
public class Person
{
 public int age;
 public String name;
 public Date birthdate;
}
Asynchronous API sample (Android and Plain Java):
final AsyncCallback<Person> updateResponder = new AsyncCallback<Person>()
{
 @Override
 public void handleResponse( Person updatedPerson )
 {
 System.out.println( "Person's name after update " + updatedPerson.name );
 }
 @Override
 public void handleFault( BackendlessFault backendlessFault )
 {
 System.out.println( "Server reported an error - " + backendlessFault.getMessage() );
 }
};
Backendless.Data.of( Person.class ).findLast( new AsyncCallback<Person>()
{
 @Override
 public void handleResponse( Person person )
 {
 System.out.println( "Loaded object. Name - " + person.name );
 person.name = "Frankie";
 Backendless.Data.of( Person.class ).save( person, updateResponder );
 }
 @Override
 public void handleFault( BackendlessFault backendlessFault )
 {
 System.out.println( "Server reported an error - " + backendlessFault.getMessage() );
 }
} );
Synchronous API sample (Plain Java only):
Person person = Backendless.Data.of( Person.class ).findFirst();
System.out.println( "Loaded object. Name - " + person.name );
person.name = "Bobby";
Person updatedPerson = Backendless.Data.of( Person.class ).save( person );
System.out.println( "Person's name after update " + updatedPerson.name );

The method which saves the object automatically checks if the object already exists in the persistent storage and if so, updates the property values. As it is shown by the example above, the method returns an updated instance back to the client.

Is article helpful?