冻结数据 - Flutter SDK
Atlas Device SDK 已弃用。 有关详细信息,请参阅弃用页面。
冻结操作会在冻结时为 Realm 中的数据创建不可变的快照。 冻结对象不是活动对象,也不会自动更新。 您无法写入冻结数据。 数据一旦被冻结,就无法解冻。
您可以冻结以下 Realm 对象类型:
冻结 Realm
使用 Realm .freeze() 创建整个域的冻结快照 。使用完冻结域后,必须将其关闭以防止内存泄漏。
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();
冻结 RealmResults
使用 RealmResults.freeze() 创建 的冻结快照RealmResults
。完成对冻结数据的处理后,必须关闭与之关联的域以防止内存泄漏。
// 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();
冻结 RealmObject
使用RealmObject
RealmObject.freeze() 创建 的冻结快照 。完成对冻结数据的处理后,必须关闭与之关联的域以防止内存泄漏。
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();
冻结 RealmObject 中的 RealmList
使用 RealmList.freeze() 在 中创建 的冻结快照 。RealmList
RealmObject
完成对冻结数据的处理后,必须关闭与之关联的域以防止内存泄漏。
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();
检查数据是否被冻结
使用isFrozen
属性检查是否有任何可冻结数据类型被冻结。 isFrozen
true
如果对象处于冻结状态, 会返回false
如果对象是活动对象,则会返回 。
// 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);