Freezing creates an immutable snapshot of data in a realm at the time of freezing. Frozen objects are not live and do not automatically update. You cannot write to frozen data. Once data is frozen, it cannot be unfrozen.
You can freeze the following object types:
Freeze a Realm
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();
Resultados de Freeze Realm
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();
Freeze a RealmObject
Create a frozen snapshot of a RealmObject with RealmObject.freeze(). Once you finish working with the frozen data, you must close the realm associated with it to prevent memory leaks.
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();
Freeze a RealmList in a RealmObject
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();
Check if Data Is Frozen
Check if any of the freezable data types are frozen with the isFrozen property. isFrozen returns true if an object is frozen and false if it is a live object.
// 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);