Skip to content

Commit

Permalink
Small fixes after PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
desistefanova committed Jan 4, 2022
1 parent ca27374 commit 9942fc7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
18 changes: 9 additions & 9 deletions lib/src/realm_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,16 @@ class Realm {
realmCore.removeRealmObject(object);
}

void removeMany<T extends RealmObject>(Iterable<T> list) {
void removeMany<T extends RealmObject>(Iterable<T> items) {
try {
if (list is RealmResults<T>) {
realmCore.realmResultsDeleteAll(list);
} else if (list is RealmList<T>) {
realmCore.realmListRemoveAll(list);
} else {
for (T realmObject in list) {
realmCore.removeRealmObject(realmObject);
}
if (items is RealmResults<T>) {
realmCore.realmResultsRemoveAll(items);
} else if (items is RealmList<T>) {
realmCore.realmListRemoveAll(items);
} else {
for (T realmObject in items) {
realmCore.removeRealmObject(realmObject);
}
}
} catch (e) {
throw RealmException("Error deleting objects from databse. Error: $e");
Expand Down
14 changes: 5 additions & 9 deletions lib/src/results.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class RealmResults<T extends RealmObject> extends collection.IterableBase<T> {
// List<T> asList() {
// return _ResultsList(this);
// }

/// Returns the index of the given object in the Results collection.
// int indexOf(T value) {
// return _results.indexOf(value);
Expand All @@ -144,7 +144,7 @@ class RealmResults<T extends RealmObject> extends collection.IterableBase<T> {
/// Returns `true` if the Results collection is empty
@override
bool get isEmpty => length == 0;

@override
Iterator<T> get iterator => RealmResultsIterator(this);

Expand All @@ -156,7 +156,7 @@ class RealmResults<T extends RealmObject> extends collection.IterableBase<T> {
/// 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;

Expand Down Expand Up @@ -215,15 +215,14 @@ class RealmResultsIterator<T extends RealmObject> implements Iterator<T> {
final Iterable<T> _iterable;
final int _length;
int _index;
T? _current;

RealmResultsIterator(Iterable<T> 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() {
Expand All @@ -232,12 +231,9 @@ class RealmResultsIterator<T extends RealmObject> implements Iterator<T> {
throw ConcurrentModificationError(_iterable);
}
if (_index >= length) {
_current = null;
return false;
}
_current = _iterable.elementAt(_index);
_index++;
return true;
}
}

26 changes: 17 additions & 9 deletions test/realm_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,10 @@ Future<void> main([List<String>? 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<Team>();
expect(teams.length, 0);
});
Expand All @@ -599,7 +601,10 @@ Future<void> main([List<String>? 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<Person>();
expect(personsFromDB.length, 0);
});
Expand All @@ -619,8 +624,7 @@ Future<void> main([List<String>? 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<Team>();
Expand All @@ -629,7 +633,10 @@ Future<void> main([List<String>? 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<Person>();
expect(personsFromDB.length, 0);
});
Expand All @@ -649,6 +656,9 @@ Future<void> main([List<String>? 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<Team>();
expect(teams.length, 0);
});
Expand All @@ -672,15 +682,13 @@ Future<void> main([List<String>? 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<RealmException>("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<Person>();
expect(personsFromDB.length, 3);

});
});
}

0 comments on commit 9942fc7

Please sign in to comment.