Atlas Device SDK uses a highly efficient storage engine to persist objects. You can create objects, update objects in the database, and eventually delete objects from the database. Because these operations modify the state of the database, we call them writes.
Transacciones de escritura
The SDK handles writes in terms of transactions. A transaction is a list of read and write operations that the SDK treats as a single indivisible operation. In other words, a transaction is all or nothing: either all of the operations in the transaction succeed or none of the operations in the transaction take effect.
All writes must happen in a transaction.
The database allows only one open transaction at a time. The SDK blocks other writes on other threads until the open transaction is complete. Consequently, there is no race condition when reading values from the database within a transaction.
When you are done with your transaction, the SDK either commits it or cancels it:
When the SDK commits a transaction, it writes all changes to disk. For synced databases, the SDK queues the change for synchronization with Atlas Device Sync.
Cuando el SDK cancela una transacción de escritura o una operación en la transacción provoca un error, todos los cambios se descartan (o se "revierten").
Operaciones de escritura
Una vez que haya abierto una base de datos, puede crear objetos dentro de ella utilizando un Reino.write() bloque de transacción.
realm.write((){ // ...write data to realm });
También puedes devolver valores de la función de retorno en la transacción de escritura.
final yoda = realm.write<Person>(() { return realm.add(Person(ObjectId(), 'Yoda')); });
Advertencia
Write RealmObjects to One Realm File
Solo puedes escribir RealmObjects A un solo archivo de dominio. Si ya escribió un RealmObject en un archivo de dominio, el SDK lanza un RealmException si intenta escribirlo en otra base de datos.
Guardados en segundo plano
You can add, modify, or delete objects asynchronously using Realm.writeAsync().
When you use Realm.writeAsync() to perform write operations, waiting to obtain the write lock and committing a transaction occur in the background. Only the write itself occurs on the main process.
This can reduce time spent blocking the execution of the main process. This is particularly useful when using Device Sync, where you don't know when and for how long the Sync client will be writing.
// Add Leia to the realm using `writeAsync` Person leia = Person(ObjectId(), "Leia"); realm.writeAsync(() { realm.add<Person>(leia); });
Create Objects
Los ejemplos de esta página utilizan dos tipos de objetos, Person y Team.
() class _Person { () late ObjectId id; late String name; late List<String> hobbies; } () class _Team { () late ObjectId id; late String name; late List<_Person> crew; late RealmValue eventLog; }
Create One Object
Para agregar un objeto a la base de datos, pasa una instancia de una clase de objeto Realm a la base de datos en un bloque de transacción de escritura con Realm.add().
realm.write(() { realm.add(Person(ObjectId(), 'Lando')); });
Create Multiple Objects
To add multiple objects to a database, pass a list of multiple objects to Realm.addAll() inside a write transaction block.
realm.write(() { realm.addAll([ Person(ObjectId(), 'Figrin D\'an'), Person(ObjectId(), 'Greedo'), Person(ObjectId(), 'Toro') ]); });