This page describes how to create specific types of objects using Atlas Device
SDK. To learn more about object models and how to define them, refer to
Define an SDK Object Model.
For more information about write transactions and the methods you can use to
create an object, refer to Create Objects.
For information about creating specific property types, including special
types, collections, and relationship properties, refer to
Create Specific Property Types.
Create Specific SDK Object Types
Before you can create a new object and persist it to the database, you must
Define an SDK Object Model. Then, include that object type in your database
schema when you open the database.
Important
Object Types Must Be in Your Schema
You can only write objects whose object type is included in the database
schema. If you try to reference or write an object of an object type
that isn't in your schema, the SDK returns a schema validation error.
Create a Realm Object
The SDK refers to the base object type as a Realm object. This identifies it
as an object type that can be managed by the device persistence layer, Realm
database.
To create an object, you must instantiate it using the realm namespace.
Move the object into the database using the
Realm.add() function
inside of a write transaction.
When you move an object into a database, this consumes the object as an
rvalue. You must use the managed object for any data access or observation.
In this example, copying the dog object into the database consumes
it as an rvalue. You can return the managed object to continue to work
with it.
The following code shows two methods for creating a new Realm object. In the
first example, we create the object first, and then add it to the realm within
a WriteAsync()
method. In the second example, we create the document within the WriteAsync
block, which returns a realm object we can further work with.
Once you've opened a database, you can create objects within it using a
Realm.write() transaction block.
realm.write((){
// ...write data to realm
});
You can also return values from the write transaction callback function.
Use realm.createObject()
in a transaction to create a persistent instance of a database object. You can
then modify the returned object with other field values using accessors and
mutators.
The following example demonstrates how to create an object with
createObject():
To add an object to a database, instantiate it as you would any other object
and then pass it to Realm.create()
inside of a write transaction. If the database schema
includes the object type and the object conforms to the schema, then the SDK
stores the object, which is now managed by the database instance.
To create a new RealmObject instance, instantiate a new object of a
realm object type.
In the following example, we instantiate a Frog object in a
realm.write() block, then pass the instantiated object to
copyToRealm() to return a managed instance:
Use realm.createObject()
in a transaction to create a persistent instance of a database object. You can
then modify the returned object with other field values using accessors and
mutators.
The following example demonstrates how to create an object with
createObject():
To add an object to a database, instantiate it as you would any other
object and then pass it to -[RLMRealm addObject:] inside
of a write transaction.
To add an object to a database, instantiate it as you would any other
object and then pass it to Realm.add(_:update:)
inside of a write transaction.
To add an object to a database, instantiate it as you would any other object
and then pass it to Realm.create() inside of a
write transaction. If the database schema includes
the object type and the object conforms to the schema, then the SDK
stores the object, which is now managed by the database instance.
// Create an object using the `realm` namespace.
auto dog = realm::Dog{.name = "Rex", .age = 1};
std::cout << "dog: " << dog.name << "\n";
// Open the database with compile-time schema checking.
auto config = realm::db_config();
auto realm = realm::db(std::move(config));
// Persist your data in a write transaction
// Optionally return the managed object to work with it immediately
auto managedDog = realm.write([&] { return realm.add(std::move(dog)); });
var testItem = new Item
{
Name = "Do this thing",
Status = ItemStatus.Open.ToString(),
Assignee = "Aimee"
};
await realm.WriteAsync(() =>
{
realm.Add(testItem);
});
// Or
var testItem2 =
await realm.WriteAsync(() =>
{
return realm.Add<Item>(new Item
{
Name = "Do this thing, too",
Status = ItemStatus.InProgress.ToString(),
Assignee = "Satya"
});
}
);
final yoda = realm.write<Person>(() {
return realm.add(Person(ObjectId(), 'Yoda'));
});
realm.executeTransaction(r -> {
// Instantiate the class using the factory function.
public ObjectId Id { get; set; } = ObjectId.GenerateNewId();
[MapTo("assignee")]
publicstring Assignee { get; set; }
[MapTo("name")]
publicstring? Name { get; set; }
[MapTo("status")]
publicstring? Status { get; set; }
}
@RealmModel()
class_Person{
@PrimaryKey()
late ObjectId id;
lateString name;
lateList<String> hobbies;
}
@RealmModel()
class_Team{
@PrimaryKey()
late ObjectId id;
lateString name;
lateList<_Person> crew;
late RealmValue eventLog;
}
// The documentation does not have this code example in Java.
// Please refer to the other languages or related pages for example code.
// The documentation does not currently have this code example in JavaScript.
// Please refer to the other languages or related pages for example code.
// The documentation does not currently have this code example in Kotlin.
// Please refer to the other languages or related pages for example code.
// The documentation does not have this code example in Kotlin for the Java SDK.
// Please refer to the other languages or related pages for example code.
// The documentation does not currently have this code example in Objective-C.
// Please refer to the other languages or related pages for example code.
// The documentation does not currently have this code example in Swift.
// Please refer to the other languages or related pages for example code.
// The documentation does not currently have this code example in TypeScript.
// Please refer to the other languages or related pages for example code.
Create an Embedded Object
To create a new embedded object instance, assign an instance of an
embedded object type to a
parent object's property. This can be in a one-to-one, one-to-many, or
inverse relationship
depending on how you defined the embedded object within the parent
object type.
Note
Embedded Objects Must Be Created Within a Parent Object
An embedded object requires a parent object and cannot exist as an
independent SDK object.
Embedded objects have strict ownership with their parent object.
After you create the embedded object, you cannot reassign it to a
different parent object or share it between multiple parent objects.
To create an embedded object, assign the raw pointer of the embedded
object to a parent object's property. Move the parent object into
the database using the Realm.add() function
inside of a write transaction.
In this example, we assign the raw pointer of the embedded object -
ContactDetails * - to the embedded object property of the parent
object - Business.contactDetails.
Then, we add the business object to the database. This copies the
business and contactDetails objects to the database.
Because ContactDetails is an embedded object, it does not have
its own lifecycle independent of the main Business object.
If you delete the Business object, this also deletes the
ContactDetails object.
To create an embedded object, assign an instance of the embedded object
to a parent object's property:
To create an embedded object, assign an instance of the embedded object
to a parent object's property:
To create an embedded object, assign an instance of the embedded object
to a parent object's property:
To create an embedded object, assign an instance of the embedded object
to a parent object's property:
In the following example, we instantiate a new Contact object with
an embedded Address, which contains a Contact object and an embedded
Country object:
realm.write {
// Instantiate a parent object with one embedded address
val contact = Contact().apply {
name = "Kermit"
address = EmbeddedAddress().apply {
propertyOwner = Contact().apply { name = "Mr. Frog" }
street = "123 Pond St"
country = EmbeddedCountry().apply { name = "United States" }
}
}
// Copy all objects to the realm to return managed instances
copyToRealm(contact)
}
We also instantiate a new Business object with a
list of embedded Address objects, which also contain Contact
objects and embedded Country objects:
To create an embedded object, assign an instance of the embedded object
to a parent object's property:
To create an embedded object, assign an instance of the embedded object
to a parent object's property:
To create an embedded object, assign an instance of the embedded object
to a parent object's property:
To create an embedded object, assign an instance of the embedded object
to a parent object's property:
// The documentation does not currently have this code example in Objective-C.
// Please refer to the other languages or related pages for example code.
// The documentation does not currently have this code example in Swift.
// Please refer to the other languages or related pages for example code.
// The documentation does not currently have this code example in TypeScript.
// Please refer to the other languages or related pages for example code.
Create an Asymmetric Object
Asymmetric objects are write-only. Once inserted, the asymmetric object syncs
to Atlas. You cannot access the managed data locally, remove it from the
database, or query for it.
For information on how to use asymmetric objects in your application,
refer to Stream Data to Atlas.
To create an asymmetric object, initialize an asymmetric object type and add
it to the database in a write transaction:
To create an asymmetric object, initialize an asymmetric object type and add
it to the database in a write transaction:
Once you have an open database, you can create an asymmetric object inside
a write transaction. Pass your object data to realm.ingest.
Data Ingest is not supported in the Java SDK.
To stream data from the client application to Atlas, use the Kotlin SDK.
To create an asymmetric object, initialize an asymmetric object type and add
it to the database in a write transaction:
Unlike other SDK objects, you do not use the copyToRealm() method to
create it. Instead, you use a special insert() extension method to insert
it into the database.