Skip to content

Commit

Permalink
RealmResultsChanges.isCleared (#1265)
Browse files Browse the repository at this point in the history
Deprecate isCleared for RealmResults.
Fixes #1189
  • Loading branch information
desistefanova committed May 25, 2023
1 parent 75a669c commit 3065a3a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
* Small (~5%) improvement when querying for a case insensitive match on a Mixed property that does not have an index.

### Fixed

* Fixed a bug that may have resulted in arrays being in different orders on different devices (Core upgrade).
* Fixed a crash when querying a mixed property with a string operator (contains/like/beginswith/endswith) or with case insensitivity (Core upgrade).
* Querying for equality of a string on an indexed mixed property was returning case insensitive matches. For example querying for `myIndexedMixed == "Foo"` would incorrectly match on values of "foo" or "FOO" etc (Core upgrade).
* Adding an index to a Mixed property on a non-empty table would crash with an assertion (Core upgrade).
* `SyncSession.pause()` could hold a reference to the database open after shutting down the sync session, preventing users from being able to delete the realm (Core upgrade).
* Fixed `RealmResultsChanges.isCleared` which was never set. It now returns `true` if the results collection is empty in the notification callback. This field is also marked as `deprecated` and will be removed in future. Use `RealmResultsChanges.results.isEmpty` instead.([#1265](https://github.com/realm/realm-dart/pull/1265)). ([#1278](https://github.com/realm/realm-dart/issues/1278)).

### Compatibility
* Realm Studio: 13.0.0 or later.
Expand Down
4 changes: 4 additions & 0 deletions lib/src/results.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ class RealmResultsChanges<T extends Object?> extends RealmCollectionChanges {
final RealmResults<T> results;

RealmResultsChanges._(super.handle, this.results);

@override
@Deprecated("`isCleared` is deprecated. Use `isEmpty` of the results collection instead.")
bool get isCleared => results.isEmpty;
}

/// @nodoc
Expand Down
16 changes: 16 additions & 0 deletions test/list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1222,4 +1222,20 @@ Future<void> main([List<String>? args]) async {
if (count > 1) fail('Should only receive one event');
}
});

test('List.asResults().isCleared notifications', () async {
final config = Configuration.local([Team.schema, Person.schema]);
final realm = getRealm(config);
final team = Team('Team 1', players: [Person('Alice'), Person('Bob')]);
realm.write(() => realm.add(team));
final playersAsResults = team.players.asResults();
expectLater(
playersAsResults.changes,
emitsInOrder(<Matcher>[
isA<RealmResultsChanges<Person>>().having((changes) => changes.inserted, 'inserted', <int>[]), // always an empty event on subscription
isA<RealmResultsChanges<Person>>().having((changes) => changes.isCleared, 'isCleared', true),
]));
realm.write(() => team.players.clear());
expect(playersAsResults.length, 0);
});
}
20 changes: 20 additions & 0 deletions test/realm_set_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -679,4 +679,24 @@ Future<void> main([List<String>? args]) async {

expect(testSets.objectsSet.asResults(), cars);
});

test('RealmSet.asResults().isCleared notifications', () {
var config = Configuration.local([TestRealmSets.schema, Car.schema]);
var realm = getRealm(config);

final cars = [Car("Tesla"), Car("Audi")];
final testSets = TestRealmSets(1)..objectsSet.addAll(cars);

realm.write(() {
realm.add(testSets);
});
final carsResult = testSets.objectsSet.asResults();
expectLater(
carsResult.changes,
emitsInOrder(<Matcher>[
isA<RealmResultsChanges<Object?>>().having((changes) => changes.inserted, 'inserted', <int>[]), // always an empty event on subscription
isA<RealmResultsChanges<Object?>>().having((changes) => changes.isCleared, 'isCleared', true),
]));
realm.write(() => testSets.objectsSet.clear());
});
}

0 comments on commit 3065a3a

Please sign in to comment.