Skip to content

Commit

Permalink
Handled error and RealmException is thrown
Browse files Browse the repository at this point in the history
  • Loading branch information
desistefanova committed Dec 30, 2021
1 parent 5bffdb0 commit ca27374
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 5 additions & 1 deletion lib/src/realm_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,18 @@ class Realm {
}

void removeMany<T extends RealmObject>(Iterable<T> list) {
if (list is RealmResults<T>) {
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);
}
}
} catch (e) {
throw RealmException("Error deleting objects from databse. Error: $e");
}
}

Expand Down
12 changes: 6 additions & 6 deletions test/realm_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -671,16 +671,16 @@ Future<void> main([List<String>? 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<RealmException>("Access to invalidated Results objects"));

final playersToDelete = teams[0].players;
expect(() => realm.write(() => {realm.close(),
realm.removeMany(playersToDelete)}),
throws<RealmException>("Error deleting objects from databse"));

//Ensure all persons still exists in DB
realm = Realm(config);
final personsFromDB = realm.all<Person>();
expect(personsFromDB.length, 3);

});
});
}

0 comments on commit ca27374

Please sign in to comment.