CRUD - Update - Java SDK
On this page
About the Examples on this Page
The examples on this page use the data model of a project
management app that has two Realm object types: Project
and Task
. A Project
has zero or more Tasks
.
See the schema for these two classes, Project
and
Task
, below:
Modify an Object
Within a transaction, you can update a Realm object the same way you would update any other object in your language of choice. Just assign a new value to the property or update the property.
The following example changes the turtle's name to "Archibald" and sets Archibald's age to 101 by assigning new values to properties:
Upsert an Object
An upsert is a write operation that either inserts a new object with a given primary key or updates an existing object that already has that primary key. We call this an upsert because it is an "update or insert" operation. This is useful when an object may or may not already exist, such as when bulk importing a dataset into an existing realm. Upserting is an elegant way to update existing entries while adding any new entries.
The following example demonstrates how to upsert an object with realm. We create a new turtle enthusiast named "Drew" and then update their name to "Andy" using insertOrUpdate():
You can also use copyToRealmOrUpdate() to
either create a new object based on a supplied object or update an
existing object with the same primary key value. Use the
CHECK_SAME_VALUES_BEFORE_SET
ImportFlag to only update fields
that are different in the supplied object:
The following example demonstrates how to insert an object or, if an object already exists with the same primary key, update only those fields that differ:
Update a Collection
Realm supports collection-wide updates. A collection update applies the same update to specific properties of several objects in a collection at once.
The following example demonstrates how to update a
collection. Thanks to the implicit inverse
relationship between the Turtle's
owner
property and the TurtleEnthusiast's turtles
property,
Realm automatically updates Josephine's list of turtles
when you use setObject()
to update the "owner" property for all turtles in the collection.
Iteration
Because realm collections always reflect the latest state, they can appear, disappear, or change while you iterate over a collection. To get a stable collection you can iterate over, you can create a snapshot of a collection's data. A snapshot guarantees the order of elements will not change, even if an element is deleted or modified.
Iterator
objects created from RealmResults
use snapshots
automatically. Iterator
objects created from RealmList
instances do not use snapshots. Use
RealmList.createSnapshot()
or
RealmResults.createSnapshot()
to manually generate a snapshot you can iterate over manually:
The following example demonstrates how to iterate over a collection
safely using either an implicit snapshot created from a RealmResults
Iterator
or a manual snapshot created from a RealmList
: