Skip to content

Commit

Permalink
Added fix for realm objects collections
Browse files Browse the repository at this point in the history
  • Loading branch information
papafe committed Jun 12, 2024
1 parent 7f4577b commit 634828a
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 9 deletions.
5 changes: 5 additions & 0 deletions packages/realm_dart/lib/src/list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ class ManagedRealmList<T extends Object?> with RealmEntity, ListMixin<T> impleme
if (isFrozen) {
throw RealmStateError('List is frozen and cannot emit changes');
}

if (keyPaths != null && _metadata == null) {
throw RealmStateError('Key paths can be used only with collections of Realm objects');
}

//TODO Also this is just called ListNotificationController, while the set one is called RealmSetNotificationController
final controller = ListNotificationsController<T>(asManaged(), keyPaths);
return controller.createStream();
Expand Down
9 changes: 5 additions & 4 deletions packages/realm_dart/lib/src/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,14 @@ class ManagedRealmMap<T extends Object?> with RealmEntity, MapMixin<String, T> i

@override
Stream<RealmMapChanges<T>> changesFor([List<String>? keyPaths]) {
// if (T is! RealmObjectBase) {
// throw RealmStateError('"Key paths can be used only with collections of Realm objects"');
// }

if (isFrozen) {
throw RealmStateError('List is frozen and cannot emit changes');
}

if (keyPaths != null && _metadata == null) {
throw RealmStateError('Key paths can be used only with collections of Realm objects');
}

final controller = MapNotificationsController<T>(asManaged(), keyPaths);
return controller.createStream();
}
Expand Down
5 changes: 5 additions & 0 deletions packages/realm_dart/lib/src/set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ class ManagedRealmSet<T extends Object?> with RealmEntity, SetMixin<T> implement
if (isFrozen) {
throw RealmStateError('Set is frozen and cannot emit changes');
}

if (keyPaths != null && _metadata == null) {
throw RealmStateError('Key paths can be used only with collections of Realm objects');
}

final controller = RealmSetNotificationsController<T>(asManaged(), keyPaths);
return controller.createStream();
}
Expand Down
3 changes: 1 addition & 2 deletions packages/realm_dart/test/list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,6 @@ void main() {
expect(() => team.players.changesFor(["test"]), throws<RealmStateError>("Unmanaged lists don't support changes"));
});

//TODO This test does not pass at the moment. We need to check how to verify that the collection is of realm objects. Maybe checking if it has metadata?
test('List.changesFor throws of collection of non-objects', () {
final config = Configuration.local([Team.schema, Person.schema]);
final realm = getRealm(config);
Expand All @@ -539,7 +538,7 @@ void main() {
var realm = getRealm(config);

final students = realm.write(() {
return realm.add(School("pizzi"));
return realm.add(School("Liceo Pizzi"));
}).students;

final externalChanges = <RealmListChanges<Student?>>[];
Expand Down
1 change: 0 additions & 1 deletion packages/realm_dart/test/realm_map_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,6 @@ void main() {
expect(() => map.changesFor(["test"]), throws<RealmStateError>("Unmanaged maps don't support changes"));
});

//TODO This test does not pass at the moment. We need to check how to verify that the collection is of realm objects. Maybe checking if it has metadata?
test('works only with realm objects', () {
final config = Configuration.local([TestRealmMaps.schema, Car.schema, EmbeddedValue.schema]);
final realm = getRealm(config);
Expand Down
1 change: 0 additions & 1 deletion packages/realm_dart/test/realm_set_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,6 @@ void main() {
}
});

//TODO This test does not pass at the moment. We need to check how to verify that the collection is of realm objects. Maybe checking if it has metadata?
test('RealmSet.changesFor throws of collection of non-objects', () {
var config = Configuration.local([TestRealmSets.schema, Car.schema]);
final realm = getRealm(config);
Expand Down
2 changes: 1 addition & 1 deletion packages/realm_dart/test/results_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ void main() {
await verifyNotifications(externalChanges, expectedNotifications: false);

subscription.cancel();
});
}, skip: true);

test('multiple keypaths', () async {
var config = Configuration.local([TestNotificationObject.schema, TestNotificationEmbeddedObject.schema, TestNotificationDifferentType.schema]);
Expand Down

0 comments on commit 634828a

Please sign in to comment.