firebase-ios-sdk icon indicating copy to clipboard operation
firebase-ios-sdk copied to clipboard

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

Open diegogarciar opened this issue 1 year ago • 3 comments

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!

diegogarciar avatar Nov 18 '24 03:11 diegogarciar

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar Nov 18 '24 03:11 google-oss-bot

The issue was first reported here https://github.com/firebase/flutterfire/issues/13822, will submit the Podfile.lock shortly!

diegogarciar avatar Nov 18 '24 03:11 diegogarciar

May be related to long-standing #12168

paulb777 avatar Nov 18 '24 21:11 paulb777

Closing in favor of https://github.com/firebase/firebase-ios-sdk/issues/12168.

ncooke3 avatar Oct 03 '25 20:10 ncooke3