Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add realm specific implementation for List.add and List.insert #894

Merged
merged 4 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
### Fixed
* Allow null arguments on query. ([#872](https://github.com/realm/realm-dart/pull/872)). Fixes [#871](https://github.com/realm/realm-dart/issues/871)
* Previously removeAt did not truncate length. ([#884](https://github.com/realm/realm-dart/pull/884)). Fixes [#883](https://github.com/realm/realm-dart/issues/883)
* List.length= now throws, if you try to increase length, ([#894](https://github.com/realm/realm-dart/pull/894)).

### Compatibility
* Realm Studio: 12.0.0 or later.
Expand Down
16 changes: 15 additions & 1 deletion lib/src/list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ class ManagedRealmList<T extends Object?> extends collection.ListBase<T> with Re
@override
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@override
/// Setting the `length` is a required method on [List], but makes less sense
/// for [RealmList]s. You can only decrease the length, increasing it throws an [RealmException].

set length(int newLength) {
var l = length;
if (newLength < l) removeRange(newLength, l);
if (newLength < l) {
removeRange(newLength, l);
} else {
throw RealmException('You cannot increase length on a realm list without adding elements');
}
}

@override
Expand Down Expand Up @@ -93,6 +97,16 @@ class ManagedRealmList<T extends Object?> extends collection.ListBase<T> with Re
}
}

@override
void add(T element) {
RealmListInternal.setValue(handle, realm, length, element);
}

@override
void insert(int index, T element) {
realmCore.listInsertElementAt(handle, index, element);
}

@override
void operator []=(int index, T value) {
RealmListInternal.setValue(handle, realm, index, value);
Expand Down
25 changes: 25 additions & 0 deletions test/list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -699,4 +699,29 @@ Future<void> main([List<String>? args]) async {
expect(team.players, isNot(players));
expect(team.players, unorderedMatches(players));
});

test('ManagedRealmList.length= throws on increase', () {
final config = Configuration.local([Team.schema, Person.schema]);
final realm = getRealm(config);

final team = Team('sad team');

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

expect(() => realm.write(() => team.players.length = 100), throws<RealmException>('You cannot increase length on a realm list without adding elements'));
});

test('ManagedRealmList.length= truncates on decrease', () {
final config = Configuration.local([Team.schema, Person.schema]);
final realm = getRealm(config);

final team = Team('sad team', players: [for (int i = 0; i < 100; ++i) Person('$i')]);
realm.write(() => realm.add(team));
expect(team.players.length, 100);
expect(realm.all<Person>().length, 100);

expect(realm.write(() => team.players.length = 10), 10);
expect(team.players.length, 10);
expect(realm.all<Person>().length, 100);
});
}