From a508a5cdfbd79a04e4e4c9e6d9960638b69bf8da Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Thu, 23 Dec 2021 17:38:06 +0200 Subject: [PATCH 01/24] Realm.RemoveMany and Results.RemoveAll implemented --- lib/src/native/realm_core.dart | 4 ++ lib/src/realm_class.dart | 10 ++++ lib/src/results.dart | 22 +++++-- test/realm_test.dart | 102 +++++++++++++++++++++++++++++++++ 4 files changed, 134 insertions(+), 4 deletions(-) diff --git a/lib/src/native/realm_core.dart b/lib/src/native/realm_core.dart index 54781a669..c2c053d6c 100644 --- a/lib/src/native/realm_core.dart +++ b/lib/src/native/realm_core.dart @@ -384,6 +384,10 @@ class _RealmCore { }); } + void realmListRemoveAll(RealmList list) { + _realmLib.invokeGetBool( + () => _realmLib.realm_list_remove_all(list.handle._pointer)); + } } class LastError { diff --git a/lib/src/realm_class.dart b/lib/src/realm_class.dart index 40b23f999..291ad5a7f 100644 --- a/lib/src/realm_class.dart +++ b/lib/src/realm_class.dart @@ -87,6 +87,16 @@ class Realm { realmCore.removeRealmObject(object); } + void removeMany(List list) { + if (list is RealmList) { + realmCore.realmListRemoveAll(list); + } else { + for (T realmObject in list) { + realmCore.removeRealmObject(realmObject); + } + } + } + bool get _isInTransaction => realmCore.getIsWritable(this); void write(void Function() writeCallback) { diff --git a/lib/src/results.dart b/lib/src/results.dart index 6ef642cf9..2226e9ce9 100644 --- a/lib/src/results.dart +++ b/lib/src/results.dart @@ -132,9 +132,22 @@ class RealmResults { // } /// Returns an [Iterable] collection for use with `for..in` - // List asList() { - // return _ResultsList(this); - // } + List asList() { + List list = []; + int itemsCount = length; + for (var i = 0; i < itemsCount; i++) { + list.add(this[i]); + } + return list; + } + + ///Removes all the objects in the results from database + void removeAll() { + int itemsCount = length; + for (var i = 0; i < itemsCount; i++) { + realmCore.removeRealmObject(this[0]); + } + } /// Returns the index of the given object in the Results collection. // int indexOf(T value) { @@ -203,7 +216,8 @@ class RealmResults { extension RealmResultsInternal on RealmResults { RealmResultsHandle get handle => _handle; - static RealmResults create(RealmResultsHandle handle, Realm realm) { + static RealmResults create( + RealmResultsHandle handle, Realm realm) { return RealmResults._(handle, realm); } } diff --git a/test/realm_test.dart b/test/realm_test.dart index 1dc4249ff..42141b2ef 100644 --- a/test/realm_test.dart +++ b/test/realm_test.dart @@ -552,5 +552,107 @@ Future main([List? args]) async { expect(() => realm.write(() => players[-1] = Person()), throws("Index out of range")); expect(() => realm.write(() => players[800] = Person()), throws()); }); + + test('Realm RemoveMany from List', () { + var config = Configuration([Team.schema, Person.schema]); + var realm = Realm(config); + + + final teamOne = Team()..name = "Ferrari"; + final teamTwo = Team()..name = "Maserati"; + realm.write(() => {realm.add(teamOne), realm.add(teamTwo)}); + + var teams = realm.all(); + expect(teams.length, 2); + + realm.write(() => realm.removeMany(teams.asList())); + teams = realm.all(); + expect(teams.length, 0); + }); + + test('Realm RemoveMany from RealmList', () { + var config = Configuration([Team.schema, Person.schema]); + var realm = Realm(config); + + final team = Team()..name = "Ferrari"; + realm.write(() => realm.add(team)); + + realm.write(() => team.players.addAll([ + Person()..name = "Michael Schumacher", + Person()..name = "Sebastian Vettel", + Person()..name = "Kimi Räikkönen" + ])); + + var teams = realm.all(); + expect(teams.length, 1); + + realm.write(() => realm.removeMany(teams[0].players)); + final personsFromDB = realm.all(); + expect(personsFromDB.length, 0); + }); + + test('Realm RemoveMany from RealmList referenced by two objects', () { + var config = Configuration([Team.schema, Person.schema]); + var realm = Realm(config); + + final teamOne = Team()..name = "Ferrari"; + final teamTwo = Team()..name = "Maserati"; + realm.write(() => {realm.add(teamOne), realm.add(teamTwo)}); + + //Create common players list for both Teams + List players = [ + Person()..name = "Michael Schumacher", + Person()..name = "Sebastian Vettel", + Person()..name = "Kimi Räikkönen" + ]; + realm.write(() => + {teamOne.players.addAll(players), teamTwo.players.addAll(players)}); + + + var teams = realm.all(); + expect(teams.length, 2); + + realm.write(() => realm.removeMany(teams[0].players)); + final personsFromDB = realm.all(); + expect(personsFromDB.length, 0); + + }); + + test('RealmResults RemoveAll', () { + var config = Configuration([Team.schema, Person.schema]); + var realm = Realm(config); + + final teamOne = Team()..name = "Ferrari"; + final teamTwo = Team()..name = "Maserati"; + realm.write(() => {realm.add(teamOne), realm.add(teamTwo)}); + + var teams = realm.all(); + expect(teams.length, 2); + + realm.write(() => teams.removeAll()); + teams = realm.all(); + expect(teams.length, 0); + }); + + test('Realm RemoveMany from RealmList', () { + var config = Configuration([Team.schema, Person.schema]); + var realm = Realm(config); + + final team = Team()..name = "Ferrari"; + realm.write(() => realm.add(team)); + + realm.write(() => team.players.addAll([ + Person()..name = "Michael Schumacher", + Person()..name = "Sebastian Vettel", + Person()..name = "Kimi Räikkönen" + ])); + + var teams = realm.all(); + expect(teams.length, 1); + + realm.write(() => realm.removeMany(teams[0].players)); + final personsFromDB = realm.all(); + expect(personsFromDB.length, 0); + }); }); } From 44b4f32cbbb05336d07dd74a050318cdc5025645 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Wed, 29 Dec 2021 21:02:03 +0200 Subject: [PATCH 02/24] RemoveAll for RealmResults --- lib/src/native/realm_core.dart | 4 +++ lib/src/realm_class.dart | 4 +++ lib/src/results.dart | 19 +++++--------- test/realm_test.dart | 48 +++++++++++++++++++++++++++------- 4 files changed, 52 insertions(+), 23 deletions(-) diff --git a/lib/src/native/realm_core.dart b/lib/src/native/realm_core.dart index c2c053d6c..7cafd070d 100644 --- a/lib/src/native/realm_core.dart +++ b/lib/src/native/realm_core.dart @@ -388,6 +388,10 @@ class _RealmCore { _realmLib.invokeGetBool( () => _realmLib.realm_list_remove_all(list.handle._pointer)); } + + void realmResultsDeleteAll(RealmResults results) { + _realmLib.invokeGetBool(() => _realmLib.realm_results_delete_all(results.handle._pointer)); + } } class LastError { diff --git a/lib/src/realm_class.dart b/lib/src/realm_class.dart index 291ad5a7f..20ceb6a89 100644 --- a/lib/src/realm_class.dart +++ b/lib/src/realm_class.dart @@ -97,6 +97,10 @@ class Realm { } } + void removeAll(RealmResults result) { + result.removeAll(); + } + bool get _isInTransaction => realmCore.getIsWritable(this); void write(void Function() writeCallback) { diff --git a/lib/src/results.dart b/lib/src/results.dart index 2226e9ce9..490687e78 100644 --- a/lib/src/results.dart +++ b/lib/src/results.dart @@ -132,21 +132,14 @@ class RealmResults { // } /// Returns an [Iterable] collection for use with `for..in` - List asList() { - List list = []; - int itemsCount = length; - for (var i = 0; i < itemsCount; i++) { - list.add(this[i]); - } - return list; - } + // List asList() { + // return _ResultsList(this); + // } + - ///Removes all the objects in the results from database + ///Removes all the objects matching results from database void removeAll() { - int itemsCount = length; - for (var i = 0; i < itemsCount; i++) { - realmCore.removeRealmObject(this[0]); - } + realmCore.realmResultsDeleteAll(this); } /// Returns the index of the given object in the Results collection. diff --git a/test/realm_test.dart b/test/realm_test.dart index 42141b2ef..0bd87a099 100644 --- a/test/realm_test.dart +++ b/test/realm_test.dart @@ -557,7 +557,7 @@ Future main([List? args]) async { var config = Configuration([Team.schema, Person.schema]); var realm = Realm(config); - + //Create two Teams final teamOne = Team()..name = "Ferrari"; final teamTwo = Team()..name = "Maserati"; realm.write(() => {realm.add(teamOne), realm.add(teamTwo)}); @@ -565,36 +565,50 @@ Future main([List? args]) async { var teams = realm.all(); expect(teams.length, 2); - realm.write(() => realm.removeMany(teams.asList())); + //Convert RealmResults to List (asList should be implemented) + List list = []; + int itemsCount = teams.length; + for (var i = 0; i < itemsCount; i++) { + list.add(teams[i]); + } + + realm.write(() => realm.removeMany(list)); teams = realm.all(); expect(teams.length, 0); }); - test('Realm RemoveMany from RealmList', () { + test('Realm RemoveMany from RealmList', () { var config = Configuration([Team.schema, Person.schema]); var realm = Realm(config); + //Create a Team final team = Team()..name = "Ferrari"; realm.write(() => realm.add(team)); + //Add players to the team realm.write(() => team.players.addAll([ Person()..name = "Michael Schumacher", Person()..name = "Sebastian Vettel", Person()..name = "Kimi Räikkönen" ])); + //Ensure the Tema exists in DB var teams = realm.all(); expect(teams.length, 1); + //Remove team players realm.write(() => realm.removeMany(teams[0].players)); + + //Ensure persons are deleted from DB final personsFromDB = realm.all(); expect(personsFromDB.length, 0); }); - test('Realm RemoveMany from RealmList referenced by two objects', () { + test('Realm RemoveMany from RealmList referenced by two objects', () { var config = Configuration([Team.schema, Person.schema]); var realm = Realm(config); + //Create two Teams final teamOne = Team()..name = "Ferrari"; final teamTwo = Team()..name = "Maserati"; realm.write(() => {realm.add(teamOne), realm.add(teamTwo)}); @@ -608,33 +622,38 @@ Future main([List? args]) async { realm.write(() => {teamOne.players.addAll(players), teamTwo.players.addAll(players)}); - + //Ensule teams exist in DB var teams = realm.all(); expect(teams.length, 2); - + + //Remove all players in a team realm.write(() => realm.removeMany(teams[0].players)); + + //Ensure all persons are deleted from DB final personsFromDB = realm.all(); expect(personsFromDB.length, 0); - }); test('RealmResults RemoveAll', () { var config = Configuration([Team.schema, Person.schema]); var realm = Realm(config); + //Create two Teams final teamOne = Team()..name = "Ferrari"; final teamTwo = Team()..name = "Maserati"; realm.write(() => {realm.add(teamOne), realm.add(teamTwo)}); + //Ensule teams exist in DB var teams = realm.all(); expect(teams.length, 2); + //Remove all objects in RealmResults from DB realm.write(() => teams.removeAll()); teams = realm.all(); expect(teams.length, 0); }); - test('Realm RemoveMany from RealmList', () { + test('Realm RemoveMany from RealmList in closed realm', () { var config = Configuration([Team.schema, Person.schema]); var realm = Realm(config); @@ -647,12 +666,21 @@ Future main([List? args]) async { Person()..name = "Kimi Räikkönen" ])); + //Ensure team exists var teams = realm.all(); expect(teams.length, 1); - realm.write(() => realm.removeMany(teams[0].players)); + //Try to delete team players while realm is closed + expect(() => realm.write(() => { + realm.close(), + realm.removeMany(teams[0].players) + }), + throws("Access to invalidated Results objects")); + + //Ensure all persons still exists in DB + realm = Realm(config); final personsFromDB = realm.all(); - expect(personsFromDB.length, 0); + expect(personsFromDB.length, 3); }); }); } From 9b178364018d5ca63ff9b902ece1de3b029fa072 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Wed, 29 Dec 2021 22:02:52 +0200 Subject: [PATCH 03/24] removed method Realm RemoveAll --- lib/src/realm_class.dart | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/src/realm_class.dart b/lib/src/realm_class.dart index 20ceb6a89..291ad5a7f 100644 --- a/lib/src/realm_class.dart +++ b/lib/src/realm_class.dart @@ -97,10 +97,6 @@ class Realm { } } - void removeAll(RealmResults result) { - result.removeAll(); - } - bool get _isInTransaction => realmCore.getIsWritable(this); void write(void Function() writeCallback) { From 86a663eae5f762817050e7bd1e9ba56cc5d24774 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Wed, 29 Dec 2021 22:28:54 +0200 Subject: [PATCH 04/24] broken line returned back --- lib/src/results.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/src/results.dart b/lib/src/results.dart index 490687e78..6d6a19b6a 100644 --- a/lib/src/results.dart +++ b/lib/src/results.dart @@ -209,8 +209,7 @@ class RealmResults { extension RealmResultsInternal on RealmResults { RealmResultsHandle get handle => _handle; - static RealmResults create( - RealmResultsHandle handle, Realm realm) { + static RealmResults create(RealmResultsHandle handle, Realm realm) { return RealmResults._(handle, realm); } } From 5bffdb05977d7aab9f9e3ccc61b34f4e6c615e65 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Thu, 30 Dec 2021 17:16:34 +0200 Subject: [PATCH 05/24] RealmResults extends IterableBase --- lib/src/realm_class.dart | 6 +++-- lib/src/results.dart | 50 +++++++++++++++++++++++++++++++--------- test/realm_test.dart | 10 ++++---- 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/lib/src/realm_class.dart b/lib/src/realm_class.dart index 291ad5a7f..7d433a8d6 100644 --- a/lib/src/realm_class.dart +++ b/lib/src/realm_class.dart @@ -87,8 +87,10 @@ class Realm { realmCore.removeRealmObject(object); } - void removeMany(List list) { - if (list is RealmList) { + void removeMany(Iterable list) { + if (list is RealmResults) { + realmCore.realmResultsDeleteAll(list); + } else if (list is RealmList) { realmCore.realmListRemoveAll(list); } else { for (T realmObject in list) { diff --git a/lib/src/results.dart b/lib/src/results.dart index 6d6a19b6a..80c0b2738 100644 --- a/lib/src/results.dart +++ b/lib/src/results.dart @@ -95,7 +95,7 @@ class _ResultsList extends collection.ListBase { /// Instances of this class are typically live collections returned by [Realm.objects] /// that will update as new objects are either added to or deleted from the Realm /// that match the underlying query. -class RealmResults { +class RealmResults extends collection.IterableBase { late final RealmResultsHandle _handle; late final Realm _realm; // RealmResults _results; @@ -136,21 +136,17 @@ class RealmResults { // return _ResultsList(this); // } - - ///Removes all the objects matching results from database - void removeAll() { - realmCore.realmResultsDeleteAll(this); - } - /// Returns the index of the given object in the Results collection. // int indexOf(T value) { // return _results.indexOf(value); // } /// Returns `true` if the Results collection is empty - bool isEmpty() { - return length == 0; - } + @override + bool get isEmpty => length == 0; + + @override + Iterator get iterator => RealmResultsIterator(this); /// Returns `true` if this Results collection has not been deleted and is part of a valid Realm. /// @@ -158,8 +154,9 @@ class RealmResults { // bool get isValid => _results.isValid(); /// Returns the number of values in the Results collection. + @override int get length => realmCore.getResultsCount(this); - + /// Returns a human-readable description of the objects contained in the collection. // String get description => _results.description; @@ -213,3 +210,34 @@ extension RealmResultsInternal on RealmResults { return RealmResults._(handle, realm); } } + +class RealmResultsIterator implements Iterator { + final Iterable _iterable; + final int _length; + int _index; + T? _current; + + RealmResultsIterator(Iterable iterable) + : _iterable = iterable, + _length = iterable.length, + _index = 0; + + @override + T get current => _current as T; + + @override + bool moveNext() { + int length = _iterable.length; + if (_length != length) { + throw ConcurrentModificationError(_iterable); + } + if (_index >= length) { + _current = null; + return false; + } + _current = _iterable.elementAt(_index); + _index++; + return true; + } +} + diff --git a/test/realm_test.dart b/test/realm_test.dart index 0bd87a099..0b998fd57 100644 --- a/test/realm_test.dart +++ b/test/realm_test.dart @@ -464,16 +464,16 @@ Future main([List? args]) async { var realm = Realm(config); var cars = realm.all(); - expect(cars.isEmpty(), true); + expect(cars.isEmpty, true); final car = Car(); realm.write(() => realm.add(car)); - expect(cars.isEmpty(), false); + expect(cars.isEmpty, false); realm.write(() => realm.remove(car)); - expect(cars.isEmpty(), true); + expect(cars.isEmpty, true); }); test('Results get by index', () { @@ -634,7 +634,7 @@ Future main([List? args]) async { expect(personsFromDB.length, 0); }); - test('RealmResults RemoveAll', () { + test('Realm RemoveMany from RealmResults', () { var config = Configuration([Team.schema, Person.schema]); var realm = Realm(config); @@ -648,7 +648,7 @@ Future main([List? args]) async { expect(teams.length, 2); //Remove all objects in RealmResults from DB - realm.write(() => teams.removeAll()); + realm.write(() => realm.removeMany(teams)); teams = realm.all(); expect(teams.length, 0); }); From ca27374331f3fdc89a4a4cedcba197170b499eff Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Thu, 30 Dec 2021 20:46:49 +0200 Subject: [PATCH 06/24] Handled error and RealmException is thrown --- lib/src/realm_class.dart | 6 +++++- test/realm_test.dart | 12 ++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/src/realm_class.dart b/lib/src/realm_class.dart index 7d433a8d6..2805edeaf 100644 --- a/lib/src/realm_class.dart +++ b/lib/src/realm_class.dart @@ -88,7 +88,8 @@ class Realm { } void removeMany(Iterable list) { - if (list is RealmResults) { + try { + if (list is RealmResults) { realmCore.realmResultsDeleteAll(list); } else if (list is RealmList) { realmCore.realmListRemoveAll(list); @@ -96,6 +97,9 @@ class Realm { for (T realmObject in list) { realmCore.removeRealmObject(realmObject); } + } + } catch (e) { + throw RealmException("Error deleting objects from databse. Error: $e"); } } diff --git a/test/realm_test.dart b/test/realm_test.dart index 0b998fd57..23789a7db 100644 --- a/test/realm_test.dart +++ b/test/realm_test.dart @@ -671,16 +671,16 @@ Future main([List? args]) async { expect(teams.length, 1); //Try to delete team players while realm is closed - expect(() => realm.write(() => { - realm.close(), - realm.removeMany(teams[0].players) - }), - throws("Access to invalidated Results objects")); - + final playersToDelete = teams[0].players; + expect(() => realm.write(() => {realm.close(), + realm.removeMany(playersToDelete)}), + throws("Error deleting objects from databse")); + //Ensure all persons still exists in DB realm = Realm(config); final personsFromDB = realm.all(); expect(personsFromDB.length, 3); + }); }); } From 9942fc76e699847cf2d39a800040f208e8006f1d Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Tue, 4 Jan 2022 13:36:19 +0200 Subject: [PATCH 07/24] Small fixes after PR comments --- lib/src/native/realm_core.dart | 2 +- lib/src/realm_class.dart | 18 +++++++++--------- lib/src/results.dart | 14 +++++--------- test/realm_test.dart | 26 +++++++++++++++++--------- 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/lib/src/native/realm_core.dart b/lib/src/native/realm_core.dart index 7cafd070d..1213e9d46 100644 --- a/lib/src/native/realm_core.dart +++ b/lib/src/native/realm_core.dart @@ -389,7 +389,7 @@ class _RealmCore { () => _realmLib.realm_list_remove_all(list.handle._pointer)); } - void realmResultsDeleteAll(RealmResults results) { + void realmResultsRemoveAll(RealmResults results) { _realmLib.invokeGetBool(() => _realmLib.realm_results_delete_all(results.handle._pointer)); } } diff --git a/lib/src/realm_class.dart b/lib/src/realm_class.dart index 2805edeaf..438687b0b 100644 --- a/lib/src/realm_class.dart +++ b/lib/src/realm_class.dart @@ -87,16 +87,16 @@ class Realm { realmCore.removeRealmObject(object); } - void removeMany(Iterable list) { + void removeMany(Iterable items) { try { - if (list is RealmResults) { - realmCore.realmResultsDeleteAll(list); - } else if (list is RealmList) { - realmCore.realmListRemoveAll(list); - } else { - for (T realmObject in list) { - realmCore.removeRealmObject(realmObject); - } + if (items is RealmResults) { + realmCore.realmResultsRemoveAll(items); + } else if (items is RealmList) { + realmCore.realmListRemoveAll(items); + } else { + for (T realmObject in items) { + realmCore.removeRealmObject(realmObject); + } } } catch (e) { throw RealmException("Error deleting objects from databse. Error: $e"); diff --git a/lib/src/results.dart b/lib/src/results.dart index 80c0b2738..00b51e1a8 100644 --- a/lib/src/results.dart +++ b/lib/src/results.dart @@ -135,7 +135,7 @@ class RealmResults extends collection.IterableBase { // List asList() { // return _ResultsList(this); // } - + /// Returns the index of the given object in the Results collection. // int indexOf(T value) { // return _results.indexOf(value); @@ -144,7 +144,7 @@ class RealmResults extends collection.IterableBase { /// Returns `true` if the Results collection is empty @override bool get isEmpty => length == 0; - + @override Iterator get iterator => RealmResultsIterator(this); @@ -156,7 +156,7 @@ class RealmResults extends collection.IterableBase { /// Returns the number of values in the Results collection. @override int get length => realmCore.getResultsCount(this); - + /// Returns a human-readable description of the objects contained in the collection. // String get description => _results.description; @@ -215,15 +215,14 @@ class RealmResultsIterator implements Iterator { final Iterable _iterable; final int _length; int _index; - T? _current; RealmResultsIterator(Iterable iterable) : _iterable = iterable, - _length = iterable.length, + _length = iterable.length, _index = 0; @override - T get current => _current as T; + T get current => (_index >= 0 && _index < _length) ? _iterable.elementAt(_index) : null as T; @override bool moveNext() { @@ -232,12 +231,9 @@ class RealmResultsIterator implements Iterator { throw ConcurrentModificationError(_iterable); } if (_index >= length) { - _current = null; return false; } - _current = _iterable.elementAt(_index); _index++; return true; } } - diff --git a/test/realm_test.dart b/test/realm_test.dart index 23789a7db..5aa96a017 100644 --- a/test/realm_test.dart +++ b/test/realm_test.dart @@ -571,8 +571,10 @@ Future main([List? args]) async { for (var i = 0; i < itemsCount; i++) { list.add(teams[i]); } - + realm.write(() => realm.removeMany(list)); + + //Reload teams from database and ensure they are removed teams = realm.all(); expect(teams.length, 0); }); @@ -599,7 +601,10 @@ Future main([List? args]) async { //Remove team players realm.write(() => realm.removeMany(teams[0].players)); - //Ensure persons are deleted from DB + //Ensure persons are deleted from collection + expect(teams[0].players.length, 0); + + //Reload persons from database and ensure they are removed final personsFromDB = realm.all(); expect(personsFromDB.length, 0); }); @@ -619,8 +624,7 @@ Future main([List? args]) async { Person()..name = "Sebastian Vettel", Person()..name = "Kimi Räikkönen" ]; - realm.write(() => - {teamOne.players.addAll(players), teamTwo.players.addAll(players)}); + realm.write(() => {teamOne.players.addAll(players), teamTwo.players.addAll(players)}); //Ensule teams exist in DB var teams = realm.all(); @@ -629,7 +633,10 @@ Future main([List? args]) async { //Remove all players in a team realm.write(() => realm.removeMany(teams[0].players)); - //Ensure all persons are deleted from DB + //Ensure all persons are deleted from collection + expect(teams[0].players.length, 0); + + //Reload persons from database and ensure they are removed final personsFromDB = realm.all(); expect(personsFromDB.length, 0); }); @@ -649,6 +656,9 @@ Future main([List? args]) async { //Remove all objects in RealmResults from DB realm.write(() => realm.removeMany(teams)); + expect(teams.length, 0); + + //Reload teams from databse and ensure they are removed teams = realm.all(); expect(teams.length, 0); }); @@ -672,15 +682,13 @@ Future main([List? args]) async { //Try to delete team players while realm is closed final playersToDelete = teams[0].players; - expect(() => realm.write(() => {realm.close(), - realm.removeMany(playersToDelete)}), + expect(() => realm.write(() => {realm.close(), realm.removeMany(playersToDelete)}), throws("Error deleting objects from databse")); - //Ensure all persons still exists in DB + //Ensure all persons still exists in database realm = Realm(config); final personsFromDB = realm.all(); expect(personsFromDB.length, 3); - }); }); } From 86e68144180eff8d4aa10ca61940ee4dc50c4b8b Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Tue, 4 Jan 2022 18:36:48 +0200 Subject: [PATCH 08/24] Fixing errors in RealmResults iterator. --- lib/src/results.dart | 17 ++++++++++------- test/realm_test.dart | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/lib/src/results.dart b/lib/src/results.dart index 00b51e1a8..86ee41f2f 100644 --- a/lib/src/results.dart +++ b/lib/src/results.dart @@ -212,27 +212,30 @@ extension RealmResultsInternal on RealmResults { } class RealmResultsIterator implements Iterator { - final Iterable _iterable; + final RealmResults _results; final int _length; int _index; + T? _current; - RealmResultsIterator(Iterable iterable) - : _iterable = iterable, - _length = iterable.length, + RealmResultsIterator(RealmResults results) + : _results = results, + _length = results.length, _index = 0; @override - T get current => (_index >= 0 && _index < _length) ? _iterable.elementAt(_index) : null as T; + T get current => _current as T; @override bool moveNext() { - int length = _iterable.length; + int length = _results.length; if (_length != length) { - throw ConcurrentModificationError(_iterable); + throw ConcurrentModificationError(_results); } if (_index >= length) { + _current = null; return false; } + _current = _results[_index]; _index++; return true; } diff --git a/test/realm_test.dart b/test/realm_test.dart index 5aa96a017..be3a8c6b5 100644 --- a/test/realm_test.dart +++ b/test/realm_test.dart @@ -690,5 +690,28 @@ Future main([List? args]) async { final personsFromDB = realm.all(); expect(personsFromDB.length, 3); }); + + test('RealmResults iteration test', () { + var config = Configuration([Team.schema, Person.schema]); + var realm = Realm(config); + + //Create two Teams + final teamOne = Team()..name = "team One"; + final teamTwo = Team()..name = "team Two"; + realm.write(() => {realm.add(teamOne), realm.add(teamTwo)}); + + //Get teams from database + var teams = realm.all(); + expect(teams.length, 2); + + //Iterate through teams and add realm objects to a list + List list = []; + for (Team team in teams) { + list.add(team); + } + + //Ensure list size is the same like teams collection + expect(list.length, teams.length); + }); }); } From 9c67ce3f8dd5edd209001a2f6f07e784962bc593 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Tue, 4 Jan 2022 19:01:22 +0200 Subject: [PATCH 09/24] private _RealmResultsIterator --- lib/src/results.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/src/results.dart b/lib/src/results.dart index 86ee41f2f..416eb0b2f 100644 --- a/lib/src/results.dart +++ b/lib/src/results.dart @@ -146,7 +146,7 @@ class RealmResults extends collection.IterableBase { bool get isEmpty => length == 0; @override - Iterator get iterator => RealmResultsIterator(this); + Iterator get iterator => _RealmResultsIterator(this); /// Returns `true` if this Results collection has not been deleted and is part of a valid Realm. /// @@ -211,13 +211,13 @@ extension RealmResultsInternal on RealmResults { } } -class RealmResultsIterator implements Iterator { +class _RealmResultsIterator implements Iterator { final RealmResults _results; final int _length; int _index; T? _current; - RealmResultsIterator(RealmResults results) + _RealmResultsIterator(RealmResults results) : _results = results, _length = results.length, _index = 0; From 2190222fe6253ba429abf5f835bd00e05a81bbc4 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Tue, 4 Jan 2022 22:06:28 +0200 Subject: [PATCH 10/24] methods summary added --- lib/src/realm_class.dart | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/src/realm_class.dart b/lib/src/realm_class.dart index 438687b0b..427ef8149 100644 --- a/lib/src/realm_class.dart +++ b/lib/src/realm_class.dart @@ -83,10 +83,18 @@ class Realm { return object; } + /// Removes given [RealmObject] from Realm database. + /// Throws [RealmException] on error. void remove(T object) { - realmCore.removeRealmObject(object); + try { + realmCore.removeRealmObject(object); + } catch (e) { + throw RealmException("Error deleting object from databse. Error: $e"); + } } + /// Removes [RealmObject] items in given collection from Realm database. + /// Throws [RealmException] on error. void removeMany(Iterable items) { try { if (items is RealmResults) { From 08eae30fab589ac4aa84693e01e9f9d23e2dd63f Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Wed, 5 Jan 2022 15:19:28 +0200 Subject: [PATCH 11/24] ConcurrentModificationError removed form _RealmResultsIterator.moveNext --- lib/src/results.dart | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/src/results.dart b/lib/src/results.dart index 416eb0b2f..cfa426b9f 100644 --- a/lib/src/results.dart +++ b/lib/src/results.dart @@ -213,13 +213,11 @@ extension RealmResultsInternal on RealmResults { class _RealmResultsIterator implements Iterator { final RealmResults _results; - final int _length; int _index; T? _current; _RealmResultsIterator(RealmResults results) : _results = results, - _length = results.length, _index = 0; @override @@ -228,9 +226,6 @@ class _RealmResultsIterator implements Iterator { @override bool moveNext() { int length = _results.length; - if (_length != length) { - throw ConcurrentModificationError(_results); - } if (_index >= length) { _current = null; return false; From e971f157cd3782c37a128a1e2fd101bd6a9b4e57 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Thu, 6 Jan 2022 16:27:15 +0200 Subject: [PATCH 12/24] Fixes after code review --- .vscode/settings.json | 3 +- lib/src/native/realm_core.dart | 3 +- lib/src/realm_class.dart | 5 -- lib/src/results.dart | 5 +- test/realm_test.dart | 107 +++++++++++++++++++-------------- 5 files changed, 68 insertions(+), 55 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 3db45b23b..ba3780745 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -96,5 +96,6 @@ "flutter/realm_flutter/ios/src/**": true, "flutter/realm_flutter/lib/**": true, "flutter/realm_flutter/test/**": true - } + }, + "dart.lineLength": 160 } \ No newline at end of file diff --git a/lib/src/native/realm_core.dart b/lib/src/native/realm_core.dart index 1213e9d46..aa938d9e7 100644 --- a/lib/src/native/realm_core.dart +++ b/lib/src/native/realm_core.dart @@ -385,8 +385,7 @@ class _RealmCore { } void realmListRemoveAll(RealmList list) { - _realmLib.invokeGetBool( - () => _realmLib.realm_list_remove_all(list.handle._pointer)); + _realmLib.invokeGetBool(() => _realmLib.realm_list_remove_all(list.handle._pointer)); } void realmResultsRemoveAll(RealmResults results) { diff --git a/lib/src/realm_class.dart b/lib/src/realm_class.dart index 427ef8149..011921169 100644 --- a/lib/src/realm_class.dart +++ b/lib/src/realm_class.dart @@ -94,9 +94,7 @@ class Realm { } /// Removes [RealmObject] items in given collection from Realm database. - /// Throws [RealmException] on error. void removeMany(Iterable items) { - try { if (items is RealmResults) { realmCore.realmResultsRemoveAll(items); } else if (items is RealmList) { @@ -104,10 +102,7 @@ class Realm { } else { for (T realmObject in items) { realmCore.removeRealmObject(realmObject); - } } - } catch (e) { - throw RealmException("Error deleting objects from databse. Error: $e"); } } diff --git a/lib/src/results.dart b/lib/src/results.dart index cfa426b9f..de8ecf2b1 100644 --- a/lib/src/results.dart +++ b/lib/src/results.dart @@ -218,7 +218,7 @@ class _RealmResultsIterator implements Iterator { _RealmResultsIterator(RealmResults results) : _results = results, - _index = 0; + _index = -1; @override T get current => _current as T; @@ -226,12 +226,13 @@ class _RealmResultsIterator implements Iterator { @override bool moveNext() { int length = _results.length; + _index++; if (_index >= length) { _current = null; return false; } _current = _results[_index]; - _index++; + return true; } } diff --git a/test/realm_test.dart b/test/realm_test.dart index be3a8c6b5..4761cd077 100644 --- a/test/realm_test.dart +++ b/test/realm_test.dart @@ -206,10 +206,10 @@ Future main([List? args]) async { test('Realm is closed', () { var config = Configuration([Car.schema]); - var realm = Realm(config); + var realm = Realm(config); expect(realm.isClosed, false); - realm.close(); + realm.close(); expect(realm.isClosed, true); }); @@ -558,10 +558,12 @@ Future main([List? args]) async { var realm = Realm(config); //Create two Teams - final teamOne = Team()..name = "Ferrari"; - final teamTwo = Team()..name = "Maserati"; - realm.write(() => {realm.add(teamOne), realm.add(teamTwo)}); + realm.write(() { + realm.add(Team()..name = "Ferrari"); + realm.add(Team()..name = "Maserati"); + }); + //Ensure the teams exist in realm var teams = realm.all(); expect(teams.length, 2); @@ -572,9 +574,10 @@ Future main([List? args]) async { list.add(teams[i]); } + //Remove objects in the list from realm realm.write(() => realm.removeMany(list)); - - //Reload teams from database and ensure they are removed + + //Reload teams from realm and ensure they are removed teams = realm.all(); expect(teams.length, 0); }); @@ -588,13 +591,14 @@ Future main([List? args]) async { realm.write(() => realm.add(team)); //Add players to the team - realm.write(() => team.players.addAll([ - Person()..name = "Michael Schumacher", - Person()..name = "Sebastian Vettel", - Person()..name = "Kimi Räikkönen" - ])); + var newPlayers = []; + newPlayers.add(Person()..name = "Michael Schumacher"); + newPlayers.add(Person()..name = "Sebastian Vettel"); + newPlayers.add(Person()..name = "Kimi Räikkönen"); - //Ensure the Tema exists in DB + realm.write(() => team.players.addAll(newPlayers)); + + //Ensure the team exists in realm var teams = realm.all(); expect(teams.length, 1); @@ -602,11 +606,11 @@ Future main([List? args]) async { realm.write(() => realm.removeMany(teams[0].players)); //Ensure persons are deleted from collection - expect(teams[0].players.length, 0); + expect(teams[0].players.length, 0); //Reload persons from database and ensure they are removed - final personsFromDB = realm.all(); - expect(personsFromDB.length, 0); + final allPersons = realm.all(); + expect(allPersons.length, 0); }); test('Realm RemoveMany from RealmList referenced by two objects', () { @@ -616,29 +620,35 @@ Future main([List? args]) async { //Create two Teams final teamOne = Team()..name = "Ferrari"; final teamTwo = Team()..name = "Maserati"; - realm.write(() => {realm.add(teamOne), realm.add(teamTwo)}); + realm.write(() { + realm.add(teamOne); + realm.add(teamTwo); + }); //Create common players list for both Teams - List players = [ - Person()..name = "Michael Schumacher", - Person()..name = "Sebastian Vettel", - Person()..name = "Kimi Räikkönen" - ]; - realm.write(() => {teamOne.players.addAll(players), teamTwo.players.addAll(players)}); + var newPlayers = []; + newPlayers.add(Person()..name = "Michael Schumacher"); + newPlayers.add(Person()..name = "Sebastian Vettel"); + newPlayers.add(Person()..name = "Kimi Räikkönen"); + + realm.write(() { + teamOne.players.addAll(newPlayers); + teamTwo.players.addAll(newPlayers); + }); //Ensule teams exist in DB var teams = realm.all(); expect(teams.length, 2); - //Remove all players in a team + //Remove all players in a team from realm realm.write(() => realm.removeMany(teams[0].players)); //Ensure all persons are deleted from collection expect(teams[0].players.length, 0); - //Reload persons from database and ensure they are removed - final personsFromDB = realm.all(); - expect(personsFromDB.length, 0); + //Reload persons from realm and ensure they are removed + final allPersons = realm.all(); + expect(allPersons.length, 0); }); test('Realm RemoveMany from RealmResults', () { @@ -646,9 +656,10 @@ Future main([List? args]) async { var realm = Realm(config); //Create two Teams - final teamOne = Team()..name = "Ferrari"; - final teamTwo = Team()..name = "Maserati"; - realm.write(() => {realm.add(teamOne), realm.add(teamTwo)}); + realm.write(() { + realm.add(Team()..name = "Ferrari"); + realm.add(Team()..name = "Maserati"); + }); //Ensule teams exist in DB var teams = realm.all(); @@ -670,25 +681,30 @@ Future main([List? args]) async { final team = Team()..name = "Ferrari"; realm.write(() => realm.add(team)); - realm.write(() => team.players.addAll([ - Person()..name = "Michael Schumacher", - Person()..name = "Sebastian Vettel", - Person()..name = "Kimi Räikkönen" - ])); + //Add players to the team + realm.write(() { + team.players.add(Person()..name = "Michael Schumacher"); + team.players.add(Person()..name = "Sebastian Vettel"); + team.players.add(Person()..name = "Kimi Räikkönen"); + }); //Ensure team exists var teams = realm.all(); expect(teams.length, 1); //Try to delete team players while realm is closed - final playersToDelete = teams[0].players; - expect(() => realm.write(() => {realm.close(), realm.removeMany(playersToDelete)}), - throws("Error deleting objects from databse")); + final players = teams[0].players; + expect( + () => realm.write(() { + realm.close(); + realm.removeMany(players); + }), + throws()); //Ensure all persons still exists in database realm = Realm(config); - final personsFromDB = realm.all(); - expect(personsFromDB.length, 3); + final allPersons = realm.all(); + expect(allPersons.length, 3); }); test('RealmResults iteration test', () { @@ -696,14 +712,15 @@ Future main([List? args]) async { var realm = Realm(config); //Create two Teams - final teamOne = Team()..name = "team One"; - final teamTwo = Team()..name = "team Two"; - realm.write(() => {realm.add(teamOne), realm.add(teamTwo)}); + realm.write(() { + realm.add(Team()..name = "team One"); + realm.add(Team()..name = "team Two"); + }); - //Get teams from database + //Get teams from realm var teams = realm.all(); expect(teams.length, 2); - + //Iterate through teams and add realm objects to a list List list = []; for (Team team in teams) { From c75a7683e674dceaabc32730ce6ad8c7d9625c19 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Thu, 6 Jan 2022 21:07:41 +0200 Subject: [PATCH 13/24] Comments in tests are corrected --- test/realm_test.dart | 50 ++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/test/realm_test.dart b/test/realm_test.dart index 4761cd077..771a547b9 100644 --- a/test/realm_test.dart +++ b/test/realm_test.dart @@ -553,11 +553,11 @@ Future main([List? args]) async { expect(() => realm.write(() => players[800] = Person()), throws()); }); - test('Realm RemoveMany from List', () { + test('Realm.removeMany from list', () { var config = Configuration([Team.schema, Person.schema]); var realm = Realm(config); - //Create two Teams + //Create two teams realm.write(() { realm.add(Team()..name = "Ferrari"); realm.add(Team()..name = "Maserati"); @@ -567,7 +567,7 @@ Future main([List? args]) async { var teams = realm.all(); expect(teams.length, 2); - //Convert RealmResults to List (asList should be implemented) + //Convert RealmResults to List (use asList if it is available) List list = []; int itemsCount = teams.length; for (var i = 0; i < itemsCount; i++) { @@ -582,11 +582,11 @@ Future main([List? args]) async { expect(teams.length, 0); }); - test('Realm RemoveMany from RealmList', () { + test('Realm.removeMany from realmList', () { var config = Configuration([Team.schema, Person.schema]); var realm = Realm(config); - //Create a Team + //Create a team final team = Team()..name = "Ferrari"; realm.write(() => realm.add(team)); @@ -595,7 +595,6 @@ Future main([List? args]) async { newPlayers.add(Person()..name = "Michael Schumacher"); newPlayers.add(Person()..name = "Sebastian Vettel"); newPlayers.add(Person()..name = "Kimi Räikkönen"); - realm.write(() => team.players.addAll(newPlayers)); //Ensure the team exists in realm @@ -605,19 +604,19 @@ Future main([List? args]) async { //Remove team players realm.write(() => realm.removeMany(teams[0].players)); - //Ensure persons are deleted from collection + //Ensure players are deleted from collection expect(teams[0].players.length, 0); - //Reload persons from database and ensure they are removed + //Reload all persons from realm and ensure they are removed final allPersons = realm.all(); expect(allPersons.length, 0); }); - test('Realm RemoveMany from RealmList referenced by two objects', () { + test('Realm.removeMany from realmList referenced by two objects', () { var config = Configuration([Team.schema, Person.schema]); var realm = Realm(config); - //Create two Teams + //Create two teams final teamOne = Team()..name = "Ferrari"; final teamTwo = Team()..name = "Maserati"; realm.write(() { @@ -625,7 +624,7 @@ Future main([List? args]) async { realm.add(teamTwo); }); - //Create common players list for both Teams + //Create common players list for both teams var newPlayers = []; newPlayers.add(Person()..name = "Michael Schumacher"); newPlayers.add(Person()..name = "Sebastian Vettel"); @@ -636,48 +635,49 @@ Future main([List? args]) async { teamTwo.players.addAll(newPlayers); }); - //Ensule teams exist in DB + //Ensule teams exist in realm var teams = realm.all(); expect(teams.length, 2); //Remove all players in a team from realm realm.write(() => realm.removeMany(teams[0].players)); - //Ensure all persons are deleted from collection + //Ensure all players are deleted from collection expect(teams[0].players.length, 0); - //Reload persons from realm and ensure they are removed + //Reload all persons from realm and ensure they are removed final allPersons = realm.all(); expect(allPersons.length, 0); }); - test('Realm RemoveMany from RealmResults', () { + test('Realm.removeMany from RealmResults', () { var config = Configuration([Team.schema, Person.schema]); var realm = Realm(config); - //Create two Teams + //Create two teams realm.write(() { realm.add(Team()..name = "Ferrari"); realm.add(Team()..name = "Maserati"); }); - //Ensule teams exist in DB + //Ensule teams exist in realm var teams = realm.all(); expect(teams.length, 2); - //Remove all objects in RealmResults from DB + //Remove all objects in realmResults from realm realm.write(() => realm.removeMany(teams)); expect(teams.length, 0); - //Reload teams from databse and ensure they are removed + //Reload teams from realm and ensure they are removed teams = realm.all(); expect(teams.length, 0); }); - test('Realm RemoveMany from RealmList in closed realm', () { + test('Realm.removeMany from realmList after realm is closed', () { var config = Configuration([Team.schema, Person.schema]); var realm = Realm(config); + //Create a team final team = Team()..name = "Ferrari"; realm.write(() => realm.add(team)); @@ -688,7 +688,7 @@ Future main([List? args]) async { team.players.add(Person()..name = "Kimi Räikkönen"); }); - //Ensure team exists + //Ensure team exists in realm var teams = realm.all(); expect(teams.length, 1); @@ -701,7 +701,7 @@ Future main([List? args]) async { }), throws()); - //Ensure all persons still exists in database + //Ensure all persons still exists in realm realm = Realm(config); final allPersons = realm.all(); expect(allPersons.length, 3); @@ -711,13 +711,13 @@ Future main([List? args]) async { var config = Configuration([Team.schema, Person.schema]); var realm = Realm(config); - //Create two Teams + //Create two teams realm.write(() { realm.add(Team()..name = "team One"); realm.add(Team()..name = "team Two"); }); - //Get teams from realm + //Reload teams from realm and ensure they exist var teams = realm.all(); expect(teams.length, 2); @@ -727,7 +727,7 @@ Future main([List? args]) async { list.add(team); } - //Ensure list size is the same like teams collection + //Ensure list size is the same like teams collection size expect(list.length, teams.length); }); }); From f1167fe50e7019ab20959a1934a9eca85a94a104 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Thu, 6 Jan 2022 21:27:22 +0200 Subject: [PATCH 14/24] Players lists in tests reordered --- test/realm_test.dart | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/test/realm_test.dart b/test/realm_test.dart index 771a547b9..434a01fd5 100644 --- a/test/realm_test.dart +++ b/test/realm_test.dart @@ -591,10 +591,11 @@ Future main([List? args]) async { realm.write(() => realm.add(team)); //Add players to the team - var newPlayers = []; - newPlayers.add(Person()..name = "Michael Schumacher"); - newPlayers.add(Person()..name = "Sebastian Vettel"); - newPlayers.add(Person()..name = "Kimi Räikkönen"); + final newPlayers = [ + Person()..name = "Michael Schumacher", + Person()..name = "Sebastian Vettel", + Person()..name = "Kimi Räikkönen", + ]; realm.write(() => team.players.addAll(newPlayers)); //Ensure the team exists in realm @@ -625,11 +626,11 @@ Future main([List? args]) async { }); //Create common players list for both teams - var newPlayers = []; - newPlayers.add(Person()..name = "Michael Schumacher"); - newPlayers.add(Person()..name = "Sebastian Vettel"); - newPlayers.add(Person()..name = "Kimi Räikkönen"); - + final newPlayers = [ + Person()..name = "Michael Schumacher", + Person()..name = "Sebastian Vettel", + Person()..name = "Kimi Räikkönen", + ]; realm.write(() { teamOne.players.addAll(newPlayers); teamTwo.players.addAll(newPlayers); @@ -682,11 +683,12 @@ Future main([List? args]) async { realm.write(() => realm.add(team)); //Add players to the team - realm.write(() { - team.players.add(Person()..name = "Michael Schumacher"); - team.players.add(Person()..name = "Sebastian Vettel"); - team.players.add(Person()..name = "Kimi Räikkönen"); - }); + final newPlayers = [ + Person()..name = "Michael Schumacher", + Person()..name = "Sebastian Vettel", + Person()..name = "Kimi Räikkönen", + ]; + realm.write(() => team.players.addAll(newPlayers)); //Ensure team exists in realm var teams = realm.all(); From dd71fa422860b08f4d88fb602ac505e88800d5d0 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Fri, 7 Jan 2022 17:19:05 +0200 Subject: [PATCH 15/24] Realm remove methods renamed to delete methods: remove=>delete, removeMany=>deleteMany --- lib/src/native/realm_core.dart | 6 ++--- lib/src/realm_class.dart | 16 +++++------ test/realm_test.dart | 49 ++++++++++++++++++---------------- 3 files changed, 37 insertions(+), 34 deletions(-) diff --git a/lib/src/native/realm_core.dart b/lib/src/native/realm_core.dart index aa938d9e7..048fa959a 100644 --- a/lib/src/native/realm_core.dart +++ b/lib/src/native/realm_core.dart @@ -317,7 +317,7 @@ class _RealmCore { }); } - void removeRealmObject(RealmObject object) { + void deleteRealmObject(RealmObject object) { _realmLib.invokeGetBool(() => _realmLib.realm_object_delete(object.handle._pointer)); } @@ -384,11 +384,11 @@ class _RealmCore { }); } - void realmListRemoveAll(RealmList list) { + void realmListDeleteAll(RealmList list) { _realmLib.invokeGetBool(() => _realmLib.realm_list_remove_all(list.handle._pointer)); } - void realmResultsRemoveAll(RealmResults results) { + void realmResultsDeleteAll(RealmResults results) { _realmLib.invokeGetBool(() => _realmLib.realm_results_delete_all(results.handle._pointer)); } } diff --git a/lib/src/realm_class.dart b/lib/src/realm_class.dart index 011921169..d023f27ac 100644 --- a/lib/src/realm_class.dart +++ b/lib/src/realm_class.dart @@ -83,25 +83,25 @@ class Realm { return object; } - /// Removes given [RealmObject] from Realm database. + /// Delete given [RealmObject] from Realm database. /// Throws [RealmException] on error. - void remove(T object) { + void delete(T object) { try { - realmCore.removeRealmObject(object); + realmCore.deleteRealmObject(object); } catch (e) { throw RealmException("Error deleting object from databse. Error: $e"); } } - /// Removes [RealmObject] items in given collection from Realm database. - void removeMany(Iterable items) { + /// Deletes [RealmObject] items in given collection from Realm database. + void deleteMany(Iterable items) { if (items is RealmResults) { - realmCore.realmResultsRemoveAll(items); + realmCore.realmResultsDeleteAll(items); } else if (items is RealmList) { - realmCore.realmListRemoveAll(items); + realmCore.realmListDeleteAll(items); } else { for (T realmObject in items) { - realmCore.removeRealmObject(realmObject); + realmCore.deleteRealmObject(realmObject); } } } diff --git a/test/realm_test.dart b/test/realm_test.dart index 434a01fd5..9a4292332 100644 --- a/test/realm_test.dart +++ b/test/realm_test.dart @@ -418,7 +418,7 @@ Future main([List? args]) async { expect(car, isNull); }); - test('Realm remove object', () { + test('Realm delete object', () { var config = Configuration([Car.schema]); var realm = Realm(config); @@ -428,7 +428,7 @@ Future main([List? args]) async { final car1 = realm.find("SomeNewNonExistingValue"); expect(car1, isNotNull); - realm.write(() => realm.remove(car1!)); + realm.write(() => realm.delete(car1!)); var car2 = realm.find("SomeNewNonExistingValue"); expect(car2, isNull); @@ -454,7 +454,7 @@ Future main([List? args]) async { expect(cars.length, 1); - realm.write(() => realm.remove(car)); + realm.write(() => realm.delete(car)); expect(cars.length, 0); }); @@ -471,7 +471,7 @@ Future main([List? args]) async { expect(cars.isEmpty, false); - realm.write(() => realm.remove(car)); + realm.write(() => realm.delete(car)); expect(cars.isEmpty, true); }); @@ -553,7 +553,7 @@ Future main([List? args]) async { expect(() => realm.write(() => players[800] = Person()), throws()); }); - test('Realm.removeMany from list', () { + test('Realm.deleteMany from list', () { var config = Configuration([Team.schema, Person.schema]); var realm = Realm(config); @@ -567,22 +567,25 @@ Future main([List? args]) async { var teams = realm.all(); expect(teams.length, 2); - //Convert RealmResults to List (use asList if it is available) + //Convert RealmResults to List + + // ignore: todo + // TODO: Use RealmResults.asList if it is available List list = []; int itemsCount = teams.length; for (var i = 0; i < itemsCount; i++) { list.add(teams[i]); } - //Remove objects in the list from realm - realm.write(() => realm.removeMany(list)); + //Delete objects in the list from realm + realm.write(() => realm.deleteMany(list)); - //Reload teams from realm and ensure they are removed + //Reload teams from realm and ensure they are deleted teams = realm.all(); expect(teams.length, 0); }); - test('Realm.removeMany from realmList', () { + test('Realm.deleteMany from realmList', () { var config = Configuration([Team.schema, Person.schema]); var realm = Realm(config); @@ -602,18 +605,18 @@ Future main([List? args]) async { var teams = realm.all(); expect(teams.length, 1); - //Remove team players - realm.write(() => realm.removeMany(teams[0].players)); + //Delete team players + realm.write(() => realm.deleteMany(teams[0].players)); //Ensure players are deleted from collection expect(teams[0].players.length, 0); - //Reload all persons from realm and ensure they are removed + //Reload all persons from realm and ensure they are deleted final allPersons = realm.all(); expect(allPersons.length, 0); }); - test('Realm.removeMany from realmList referenced by two objects', () { + test('Realm.deleteMany from realmList referenced by two objects', () { var config = Configuration([Team.schema, Person.schema]); var realm = Realm(config); @@ -640,18 +643,18 @@ Future main([List? args]) async { var teams = realm.all(); expect(teams.length, 2); - //Remove all players in a team from realm - realm.write(() => realm.removeMany(teams[0].players)); + //Delete all players in a team from realm + realm.write(() => realm.deleteMany(teams[0].players)); //Ensure all players are deleted from collection expect(teams[0].players.length, 0); - //Reload all persons from realm and ensure they are removed + //Reload all persons from realm and ensure they are deleted final allPersons = realm.all(); expect(allPersons.length, 0); }); - test('Realm.removeMany from RealmResults', () { + test('Realm.deleteMany from RealmResults', () { var config = Configuration([Team.schema, Person.schema]); var realm = Realm(config); @@ -665,16 +668,16 @@ Future main([List? args]) async { var teams = realm.all(); expect(teams.length, 2); - //Remove all objects in realmResults from realm - realm.write(() => realm.removeMany(teams)); + //Delete all objects in realmResults from realm + realm.write(() => realm.deleteMany(teams)); expect(teams.length, 0); - //Reload teams from realm and ensure they are removed + //Reload teams from realm and ensure they are deleted teams = realm.all(); expect(teams.length, 0); }); - test('Realm.removeMany from realmList after realm is closed', () { + test('Realm.deleteMany from realmList after realm is closed', () { var config = Configuration([Team.schema, Person.schema]); var realm = Realm(config); @@ -699,7 +702,7 @@ Future main([List? args]) async { expect( () => realm.write(() { realm.close(); - realm.removeMany(players); + realm.deleteMany(players); }), throws()); From 1dfbd021e8009877d29a21b4b6852132a75ebaac Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Fri, 7 Jan 2022 21:24:04 +0200 Subject: [PATCH 16/24] realm_results_snapshot added to c library and ios --- flutter/realm_flutter/ios/Classes/RealmPlugin.m | 1 + src/realm_dart.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/flutter/realm_flutter/ios/Classes/RealmPlugin.m b/flutter/realm_flutter/ios/Classes/RealmPlugin.m index 2b378dac6..0e6a9d563 100644 --- a/flutter/realm_flutter/ios/Classes/RealmPlugin.m +++ b/flutter/realm_flutter/ios/Classes/RealmPlugin.m @@ -40,6 +40,7 @@ void dummy(void) { realm_object_create(NULL, 0); realm_dart_get_files_path(); realm_results_get_object(NULL, 0); + realm_results_snapshot(nullptr); } @end diff --git a/src/realm_dart.cpp b/src/realm_dart.cpp index c971ea954..38c365b61 100644 --- a/src/realm_dart.cpp +++ b/src/realm_dart.cpp @@ -62,6 +62,7 @@ void dummy(void) { realm_get_library_version(); realm_object_create(nullptr, 0); realm_results_get_object(nullptr, 0); + realm_results_snapshot(nullptr); #if (ANDROID) realm_android_dummy(); #endif From e5c5dddf82809768d0e2c4b26ea840cd9986c4e5 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Mon, 10 Jan 2022 15:48:21 +0200 Subject: [PATCH 17/24] C API realm_results_snapshot deleted back from dummy --- flutter/realm_flutter/ios/Classes/RealmPlugin.m | 1 - src/realm_dart.cpp | 1 - 2 files changed, 2 deletions(-) diff --git a/flutter/realm_flutter/ios/Classes/RealmPlugin.m b/flutter/realm_flutter/ios/Classes/RealmPlugin.m index 0e6a9d563..2b378dac6 100644 --- a/flutter/realm_flutter/ios/Classes/RealmPlugin.m +++ b/flutter/realm_flutter/ios/Classes/RealmPlugin.m @@ -40,7 +40,6 @@ void dummy(void) { realm_object_create(NULL, 0); realm_dart_get_files_path(); realm_results_get_object(NULL, 0); - realm_results_snapshot(nullptr); } @end diff --git a/src/realm_dart.cpp b/src/realm_dart.cpp index 38c365b61..c971ea954 100644 --- a/src/realm_dart.cpp +++ b/src/realm_dart.cpp @@ -62,7 +62,6 @@ void dummy(void) { realm_get_library_version(); realm_object_create(nullptr, 0); realm_results_get_object(nullptr, 0); - realm_results_snapshot(nullptr); #if (ANDROID) realm_android_dummy(); #endif From f1683d56e5df0e121c3cb13f1078ca04f643a09a Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Mon, 10 Jan 2022 16:06:18 +0200 Subject: [PATCH 18/24] Test "Realm.deleteMany from list" fixes after code review --- test/realm_test.dart | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/test/realm_test.dart b/test/realm_test.dart index 9a4292332..96c27444d 100644 --- a/test/realm_test.dart +++ b/test/realm_test.dart @@ -558,31 +558,25 @@ Future main([List? args]) async { var realm = Realm(config); //Create two teams + final teamOne = Team()..name = "Team one"; + final teamTwo = Team()..name = "Team two"; + final teamThree = Team()..name = "Team three"; realm.write(() { - realm.add(Team()..name = "Ferrari"); - realm.add(Team()..name = "Maserati"); + realm.add(teamOne); + realm.add(teamTwo); + realm.add(teamThree); }); //Ensure the teams exist in realm var teams = realm.all(); - expect(teams.length, 2); + expect(teams.length, 3); - //Convert RealmResults to List + //Delete teams one and three from realm + realm.write(() => realm.deleteMany([teamOne, teamThree])); - // ignore: todo - // TODO: Use RealmResults.asList if it is available - List list = []; - int itemsCount = teams.length; - for (var i = 0; i < itemsCount; i++) { - list.add(teams[i]); - } - - //Delete objects in the list from realm - realm.write(() => realm.deleteMany(list)); - - //Reload teams from realm and ensure they are deleted - teams = realm.all(); - expect(teams.length, 0); + //Ensure both teams are deleted and only teamTwo has left + expect(teams.length, 1); + expect(teams[0].name, teamTwo.name); }); test('Realm.deleteMany from realmList', () { From d699018886a4ba7d1d2837623d6a595098671531 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova <95419820+desistefanova@users.noreply.github.com> Date: Tue, 11 Jan 2022 14:30:55 +0200 Subject: [PATCH 19/24] Update lib/src/native/realm_core.dart Co-authored-by: blagoev --- lib/src/native/realm_core.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/native/realm_core.dart b/lib/src/native/realm_core.dart index 048fa959a..ae6d29cbc 100644 --- a/lib/src/native/realm_core.dart +++ b/lib/src/native/realm_core.dart @@ -388,7 +388,7 @@ class _RealmCore { _realmLib.invokeGetBool(() => _realmLib.realm_list_remove_all(list.handle._pointer)); } - void realmResultsDeleteAll(RealmResults results) { + void resultsDeleteAll(RealmResults results) { _realmLib.invokeGetBool(() => _realmLib.realm_results_delete_all(results.handle._pointer)); } } From 82b355d410548f221a23d48978b1536382617657 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova <95419820+desistefanova@users.noreply.github.com> Date: Tue, 11 Jan 2022 14:31:30 +0200 Subject: [PATCH 20/24] Update lib/src/native/realm_core.dart Co-authored-by: blagoev --- lib/src/native/realm_core.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/native/realm_core.dart b/lib/src/native/realm_core.dart index ae6d29cbc..188695d23 100644 --- a/lib/src/native/realm_core.dart +++ b/lib/src/native/realm_core.dart @@ -384,7 +384,7 @@ class _RealmCore { }); } - void realmListDeleteAll(RealmList list) { + void listDeleteAll(RealmList list) { _realmLib.invokeGetBool(() => _realmLib.realm_list_remove_all(list.handle._pointer)); } From f84a2a1c667463a2e24bab897b674c255a69ead9 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Tue, 11 Jan 2022 14:36:09 +0200 Subject: [PATCH 21/24] Methods renamed realmResultsDeleteAll ->resultsDeleteAll and realmListDeleteAll ->listDeleteAll --- lib/src/realm_class.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/realm_class.dart b/lib/src/realm_class.dart index d023f27ac..13652787c 100644 --- a/lib/src/realm_class.dart +++ b/lib/src/realm_class.dart @@ -96,9 +96,9 @@ class Realm { /// Deletes [RealmObject] items in given collection from Realm database. void deleteMany(Iterable items) { if (items is RealmResults) { - realmCore.realmResultsDeleteAll(items); + realmCore.resultsDeleteAll(items); } else if (items is RealmList) { - realmCore.realmListDeleteAll(items); + realmCore.listDeleteAll(items); } else { for (T realmObject in items) { realmCore.deleteRealmObject(realmObject); From f6d4bbd1bf583a3fcaf5901b9c99b5d8f90b82cd Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Tue, 11 Jan 2022 14:50:16 +0200 Subject: [PATCH 22/24] merge --- lib/src/realm_class.dart | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/src/realm_class.dart b/lib/src/realm_class.dart index c85dfc7db..e90624400 100644 --- a/lib/src/realm_class.dart +++ b/lib/src/realm_class.dart @@ -95,15 +95,16 @@ class Realm { /// Deletes [RealmObject] items in given collection from Realm database. void deleteMany(Iterable items) { - if (items is RealmResults) { + if (items is RealmResults) { realmCore.resultsDeleteAll(items); - } else if (items is RealmList) { + } else if (items is RealmList) { realmCore.listDeleteAll(items); - } else { - for (T realmObject in items) { + } else { + for (T realmObject in items) { realmCore.deleteRealmObject(realmObject); } } + } void addAll(Iterable items) { for (final i in items) { @@ -112,7 +113,7 @@ class Realm { } void remove(T object) { - realmCore.removeRealmObject(object); + realmCore.deleteRealmObject(object); } bool get _isInTransaction => realmCore.getIsWritable(this); From f99a7ceb5577209c5fecdf0fb82c40c8a2ecbbe6 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Tue, 11 Jan 2022 16:38:28 +0200 Subject: [PATCH 23/24] Fixed test 'Realm.deleteMany from realmList after realm is closed' --- test/realm_test.dart | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/realm_test.dart b/test/realm_test.dart index 1799ffdd2..de1917544 100644 --- a/test/realm_test.dart +++ b/test/realm_test.dart @@ -818,7 +818,7 @@ Future main([List? args]) async { expect(teams[0].players.length, 1); var players = teams[0].players; - + realm.close(); expect( () => realm.write(() { @@ -827,15 +827,15 @@ Future main([List? args]) async { throws()); realm = Realm(config); - + //Teams must be reloaded since realm was reopened teams = realm.all(); - + //Ensure that the team is still related to the player expect(teams.length, 1); expect(teams[0].players.length, 1); }); - + test('Realm.deleteMany from list', () { var config = Configuration([Team.schema, Person.schema]); var realm = Realm(config); @@ -976,9 +976,9 @@ Future main([List? args]) async { //Try to delete team players while realm is closed final players = teams[0].players; + realm.close(); expect( () => realm.write(() { - realm.close(); realm.deleteMany(players); }), throws()); From b4394ea0867868f402c5a117d1ddea65a4e3b89c Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Tue, 11 Jan 2022 19:08:17 +0200 Subject: [PATCH 24/24] test name changed --- test/realm_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/realm_test.dart b/test/realm_test.dart index de1917544..6d075d11a 100644 --- a/test/realm_test.dart +++ b/test/realm_test.dart @@ -800,7 +800,7 @@ Future main([List? args]) async { expect(allPlayers.length, 1); }); - test('RealmList clear - exception', () { + test('RealmList clear in closed realm - expected exception', () { var config = Configuration([Team.schema, Person.schema]); var realm = Realm(config);