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

deleteMany Ignores _skipOffset in RealmResults #1832

Open
AlexeyKatsuro opened this issue Mar 3, 2025 · 0 comments
Open

deleteMany Ignores _skipOffset in RealmResults #1832

AlexeyKatsuro opened this issue Mar 3, 2025 · 0 comments

Comments

@AlexeyKatsuro
Copy link

AlexeyKatsuro commented Mar 3, 2025

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();
  });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant