データの固定 - Flutter SDK
Atlas Device SDK は非推奨です。 詳細については、 の廃止ページを参照してください。
停止時に、Realm 内のデータの不変のスナップショットが作成されます。 凍結されたオブジェクトはライブではなく、自動的に更新されません。 固定されたデータには書込み (write) はできません。 データが固定されると、解凍することはできません。
次のオブジェクトタイプを固定化できます。
Realm の固定
Realm 邦土() を使用して Realm 全体の固定スナップショットを作成する 。固定された邦土の操作が終了したら、メモリ リークを防ぐために閉じる必要があります。
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
。固定データの操作が終了したら、メモリ リークを防ぐために、そのデータに関連付けられている Realm を閉じる必要があります。
// 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();
Realm オブジェクトの固定
RealmObject.freeze() を使用して の固定スナップショットを作成する 。RealmObject
固定データの操作が終了したら、メモリ リークを防ぐために、そのデータに関連付けられている Realm を閉じる必要があります。
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
固定データの操作が終了したら、メモリ リークを防ぐために、そのデータに関連付けられている Realm を閉じる必要があります。
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);