Progress will discontinue Telerik Platform on May 10th, 2018. Learn more

Updating Data Items

Updating Data Items

The Backend Services Android SDK allows you to update single or multiple data items in a single DataItem class method call. You can use either updateById() to update a single item by ID or update() to update one, multiple, or all items.

Before you can use the update methods, you need to implement additional code in your data class. Assume you have a content type named Book and you want to be able to update its Title. You need to be able to tell the SDK that the field has changed so that it can send it to the server. There are two ways to implement this:

  • Tweak the default implementation of the SetProperty() method by using the DataItem.onPropertyChanged() SDK method.
public void setTitle(String title) {
    this.onPropertyChanged("title", this.title, this.title = title);
}
  • Update DataItem.changedProperies when your property is updated:
public void setTitle(String title) {
    this.getChangedProperties().add("title");
}

You don't need to implement those when updating system fields such as ModifiedAt and CreatedBy.

Finally, you can call update() to update the data field:

Book book = app.workWith().data(Book.class).getById(bookId).executeSync();
book.setTitle("New title");

app.workWith().data(Book.class).update(book).executeAsync();

The code above sends an update query to the server, including only the modified fields in the request.

In case you call onPropertyChanged(), it automatically checks the values for changes. On the other hand, adding a property name inside DataItem.changedProperies solely marks the property as changed. If you decide to use it, you have to set the property and make any comparisons yourself.

Update by ID

Now that you know what you need for updating to work, here is an example of how to execute the actual update request:

public void updateBookById(EverliveApp app, UUID bookId, Book updatedBook) {
    app.workWith().data(Book.class).updateById(bookId, updatedBook).executeAsync();
}

You can update system fields such as CreatedAt and CreatedBy. However, you can set them only after doing the following:

Updating Multiple Items

If you want to update multiple items, you can use single operation. You can also update all items or use a filter (more information about filters here.

public void updateBooksByFilter(EverliveApp app, UUID userId) {
   Book newBook = new Book();
   newBook.setTitle("Changed title");
   app.workWith().data(Book.class).
      update(newBook).
      where(new ValueCondition("UserId", userId, ValueConditionOperator.EqualTo)).
      executeAsync();
}

Be careful when updating fields marked as unique (IsUnique) by filter as it may match multiple items. In this case, Telerik Platform updates the first matching item if the update value does not duplicate an existing item, after which it stops executing the request and returns an error. The rest of the matching items are not updated.

Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.