-
Notifications
You must be signed in to change notification settings - Fork 111
Open
Description
What happened?
The deleteMany method in Realm Dart does not respect the _skipOffset when called on a RealmResults object that was modified using .skip(). As a result, it deletes all elements instead of only those that should be included after skipping.
Root Cause
- The
skip()method creates a newRealmResultswith an internal_skipOffset. - The
deleteManyimplementation calls:if (items is RealmResults<T>) { _ensureManagedByThis(items, 'delete objects from Realm'); items.handle.deleteAll(); }
items.handle.deleteAll();does not account for_skipOffset, leading to all items being deleted.
Expected Behavior
deleteMany(realm.all<Item>().skip(n)) should delete only the remaining items after skipping, not the entire collection.
Repro steps
- Create an in-memory Realm instance.
- Add two
Itemobjects. - Call
realm.all<Item>().skip(1), which returns aRealmResultswith an internal_skipOffset. - Pass the result to
deleteMany. - Expectation: The second item should be deleted, as skip(1) skips the first item and results in the second item being the only one left.
- Actual behavior:
deleteManyremoves all items.
Version
Flutter 3.29.0 • Dart 3.7.0
What Atlas Services are you using?
Local Database only
What type of application is this?
Dart standalone application
Client OS and version
MacOS
Code snippets
@RealmModel()
class _Item {
@PrimaryKey()
late int id;
}
void main() {
test('deleteMany should respect skip offset', () {
final realm = Realm(Configuration.inMemory([Item.schema]));
realm.write(() {
realm.add(Item(0));
realm.add(Item(1));
});
realm.write(() {
realm.deleteMany(realm.all<Item>().skip(1));
});
expect(realm.all<Item>().length, 1); // Fails: All items are deleted
realm.close();
});
}