Skip to content

Commit

Permalink
Add RealmResults.isValid (#1231)
Browse files Browse the repository at this point in the history
* Add RealmResults.isValid

* Add tests

* Update CHANGELOG
  • Loading branch information
nielsenko committed May 25, 2023
1 parent 093c535 commit 188db09
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## vNext (TBD)

### Enhancements
* Add `RealmResults.isValid` ([#1231](https://github.com/realm/realm-dart/pull/1231)).
* Support `Decimal128` datatype ([#1192](https://github.com/realm/realm-dart/pull/1192)).
* Realm logging is extended to support logging of all Realm storage level messages. (Core upgrade).
* Realm.logger now prints by default to the console from the first Isolate that initializes a Realm in the application. ([#1226](https://github.com/realm/realm-dart/pull/1226)).
Expand Down
8 changes: 8 additions & 0 deletions lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,14 @@ class _RealmCore {
});
}

bool resultsIsValid(RealmResults results) {
return using((arena) {
final is_valid = arena<Bool>();
_realmLib.invokeGetBool(() => _realmLib.realm_results_is_valid(results.handle._pointer, is_valid));
return is_valid.value;
});
}

CollectionChanges getCollectionChanges(RealmCollectionChangesHandle changes) {
return using((arena) {
final out_num_deletions = arena<Size>();
Expand Down
3 changes: 3 additions & 0 deletions lib/src/results.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class RealmResults<T extends Object?> extends collection.IterableBase<T> with Re
setRealm(realm);
}

/// Gets a value indicating whether this collection is still valid to use.
bool get isValid => realmCore.resultsIsValid(this);

/// Returns the element of type `T` at the specified [index].
T operator [](int index) => elementAt(index);

Expand Down
33 changes: 33 additions & 0 deletions test/results_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,39 @@ Future<void> main([List<String>? args]) async {
}
});

test('RealmResults.isValid', () {
final config = Configuration.local([Team.schema, Person.schema]);
final realm = getRealm(config);

final alice = Person('Alice');
final bob = Person('Bob');
final carol = Person('Carol');
final dan = Person('Dan');

final team = realm.write(() {
return realm.add(Team('Class of 92', players: [alice, bob, carol, dan]));
});

final players = team.players;
final playersAsResults = team.players.asResults();

expect(players.isValid, isTrue);
expect(playersAsResults.isValid, isTrue);
expect(playersAsResults, [alice, bob, carol, dan]);

realm.write(() => realm.delete(team));

expect(team.isValid, isFalse); // dead object
expect(players.isValid, isFalse); // parent is dead
expect(() => players.isEmpty, throwsException); // illegal to access properties on dead object
expect(playersAsResults.isValid, isTrue); // Results are still valid..
expect(playersAsResults, isEmpty); // .. but obviously empty
expect(() => playersAsResults.freeze(), returnsNormally);
final frozeResults = playersAsResults.freeze();
expect(frozeResults.isFrozen, isTrue);
expect(frozeResults, isEmpty);
});

test('query by condition on decimal128', () {
final config = Configuration.local([ObjectWithDecimal.schema]);
final realm = getRealm(config);
Expand Down

0 comments on commit 188db09

Please sign in to comment.