Skip to content

deleteMany Ignores _skipOffset in RealmResults #1832

@AlexeyKatsuro

Description

@AlexeyKatsuro

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 new RealmResults with an internal _skipOffset.
  • The deleteMany implementation 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

  1. Create an in-memory Realm instance.
  2. Add two Item objects.
  3. Call realm.all<Item>().skip(1), which returns a RealmResults with an internal _skipOffset.
  4. Pass the result to deleteMany.
  5. 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.
  6. Actual behavior: deleteMany removes 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();
  });
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions