Docs Menu
Docs Home
/ /
Base de datos de reinos

Congelar datos - SDK de Flutter

La congelación crea una instantánea inmutable de los datos de un reino en el momento de la congelación. Los objetos congelados no están activos ni se actualizan automáticamente. No se puede escribir en los datos congelados. Una vez congelados, los datos no se pueden descongelar.

Puede congelar los siguientes tipos de objetos:

  • Realm

  • Resultados del reino

  • Objeto de reino

  • Lista de reinos

Crea una instantánea congelada de un reino completo con Realm.freeze()Una vez que termine de trabajar con el reino congelado, debe cerrarlo para evitar pérdidas de memoria.

final config = Configuration.local([Person.schema, Scooter.schema]);
final realm = Realm(config);
// Add scooter owned by Mace Windu
final maceWindu = Person(ObjectId(), "Mace", "Windu");
final purpleScooter =
Scooter(ObjectId(), "Purple scooter", owner: maceWindu);
realm.write(() {
realm.add(purpleScooter);
});
// Create frozen snapshot of realm
final frozenRealm = realm.freeze();
// Update data in the realm
final quiGonJinn = Person(ObjectId(), "Qui-Gon", "Jinn");
realm.write(() {
purpleScooter.owner = quiGonJinn;
});
// Data changes not in the frozen snapshot
final purpleScooterFrozen =
frozenRealm.query<Scooter>("name == \$0", ["Purple scooter"]).first;
print(purpleScooterFrozen.owner!.firstName); // prints 'Mace'
// You must also close the frozen realm before exiting the process
frozenRealm.close();

Crear una instantánea congelada de RealmResults Con RealmResults.freeze(). Una vez que termine de trabajar con los datos congelados, debe cerrar el dominio asociado para evitar fugas de memoria.

// Add data to the realm
final maceWindu = Person(ObjectId(), "Mace", "Windu");
final jocastaNu = Person(ObjectId(), "Jocasta", "Nu");
realm.write(() => realm.addAll([maceWindu, jocastaNu]));
// Get RealmResults and freeze data
final people = realm.all<Person>();
final frozenPeople = people.freeze();
// Update data in the non-frozen realm
final newLastName = "Foo";
realm.write(() {
for (var person in people) {
person.lastName = newLastName;
}
});
// Data changes not in the frozen snapshot
final frozenFooPeople =
frozenPeople.query("lastName == \$0", [newLastName]);
print(frozenFooPeople.length); // prints 0
// You must also close the frozen realm associated
// with the frozen RealmResults before exiting the process
frozenPeople.realm.close();

Cree una instantánea congelada de un RealmObject con RealmObject.freeze(). Una vez que termine de trabajar con los datos congelados, debe cerrar el dominio asociado para evitar fugas de memoria.

final person = realm.query<Person>(
'firstName == \$0 AND lastName == \$1', ["Count", "Dooku"]).first;
// Freeze RealmObject
final frozenPerson = person.freeze();
// Change data in the unfrozen object.
realm.write(() {
realm.delete(person);
});
// Frozen person snapshot still exists even though data deleted
// in the unfrozen realm
print(frozenPerson.isValid); // prints true
print(person.isValid); // prints false
// You must also close the frozen realm associated
// with the frozen RealmObject before exiting the process
frozenPerson.realm.close();

Crea una instantánea congelada de RealmList en un RealmObject con RealmList.freeze(). Una vez que termines de trabajar con los datos congelados, debes cerrar el dominio asociado para evitar fugas de memoria.

final firstPerson =
realm.query<Person>("firstName = \$0", ["Yoda"]).first;
// Freeze RealmList in a RealmObject
final firstPersonAttributesFrozen = firstPerson.attributes.freeze();
// Change data in the unfrozen realm
final newAttribute = "quick";
realm.write(() {
// Append item to list
firstPerson.attributes.add(newAttribute);
});
final index = firstPersonAttributesFrozen.indexOf(newAttribute);
print(index); // prints -1 because cannot find new attribute
// You must also close the frozen realm associated
// with the frozen RealmList before exiting the process
firstPersonAttributesFrozen.realm.close();

Compruebe si alguno de los tipos de datos congelables está congelado con la propiedad isFrozen. isFrozen devuelve true si un objeto está congelado y false si es un objeto activo.

// You can check if all freezable types are frozen
// with the `isFrozen` property.
final realm = Realm(config);
print(realm.isFrozen);
final people = realm.all<Person>();
print(people.isFrozen);
final firstPerson =
realm.query<Person>("firstName = \$0", ["Yoda"]).first;
print(firstPerson.isFrozen);
final firstPersonAttributes = firstPerson.attributes;
print(firstPersonAttributes.isFrozen);

Volver

Reaccionar a los cambios

En esta página