Skip to content

Realtime Database: Database listener clears children when performing another query in same ref #14137

Open
@diegogarciar

Description

@diegogarciar

Description

For a chat application I am doing a double query, I have a listener for new messages, and a paginated query for old messages.

I just discovered that my listener is impacted by the queries I do on that same path, which I believe shouldn't be the case

Reproducing the issue

  final database = FirebaseDatabase.instance;

  final path = '/test_path';
  String? randomKey;
  for (var i = 0; i < 10; i++) {
    final ref = database.ref(path).push();
    if (i == 5) {
      randomKey = ref.key;
    }
    await ref.set('test $i');
  }

  final ref = database.ref(path);
  var hasQueried = false;
  ref.orderByKey().startAt(randomKey).onValue.listen((event) {
    print('listener  ${event.snapshot.children.length}');

    if (!hasQueried) {
      hasQueried = true;
      ref.orderByKey().limitToLast(3).endBefore(randomKey).get().then((data) {
        print('obtained previous messages ${data.children.length}');
      });
    }
  });

Firebase SDK Version

11.2

Xcode Version

16.1

Installation Method

CocoaPods

Firebase Product(s)

Database

Targeted Platforms

iOS

Relevant Log Output

listener  5
obtained previous messages 3
listener  0

If using Swift Package Manager, the project's Package.resolved

Expand Package.resolved snippet
Replace this line with the contents of your Package.resolved.

If using CocoaPods, the project's Podfile.lock

Expand Podfile.lock snippet
Replace this line with the contents of your Podfile.lock!

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