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

Possible bug in “queryOrdered(byChild:).queryEqual(toValue:).getData" function #8286

Open
fawzialrifai opened this issue Jun 22, 2021 · 6 comments
Assignees

Comments

@fawzialrifai
Copy link

fawzialrifai commented Jun 22, 2021

[REQUIRED] Step 1: Describe your environment

  • Xcode version: Version 12.5 (12E262)
  • Firebase SDK version: 8.1.1
  • Installation method: Swift Package Manager
  • Firebase Component: Database

[REQUIRED] Step 2: Describe the problem

Steps to reproduce:

What happened? How can we make the problem occur?
I’m observing how many children a person called "james" has using child("james").child("children").observe(.value) function, in my case he has 2 children called "child1" and "child2" so it prints children count = 2. Then I check if "james" has a child called "child2" using child("james").child("children").queryOrdered(byChild: "name").queryEqual(toValue: "child2").getData function, in my case he has a child called "child2" so it prints found. Then unexpectedly the first observe function that observes and prints children count triggers again and prints children count = 1. I think the first observe function should not trigger and the children’s count should remain at 2. If I replace getData function with observe function to search for a child named "child2" it finds it and prints found and the first observe function that observes and prints children count does not trigger and the children count remains 2.

If you have a downloadable sample project that reproduces the bug you're reporting, you will
likely receive a faster response on your issue.
https://github.com/fawzialrifai/People

Relevant Code:

import UIKit
import Firebase

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        Database.database().reference().child("james").child("children").observe(.value) { snapshot in
            print("number of james children = \(snapshot.childrenCount)") // observe and print james children count
        }
        Database.database().reference().child("james").child("children").queryOrdered(byChild: "name").queryEqual(toValue: "child2").getData { error, snapshot in
            if snapshot.exists() {  // search for james child named child2
                print("found")
            } else {
                print("not found")
            }
        }
    }

}

Screen Shot 2021-06-22 at 10 46 31 PM

Screen Shot 2021-06-22 at 11 39 15 PM

Screen Shot 2021-06-22 at 10 53 35 PM

@jmwski
Copy link
Contributor

jmwski commented Sep 3, 2021

Apologies for the delay on this. It looks like you're using v8.1.1 of the SDK. Could you try >= v8.5.0, which contains an important fix for the getData endpoint?

FWIW, I tried to reproduce the issue you've described in this draft PR: https://github.com/firebase/firebase-ios-sdk/pull/8618/files and was not able to get the observer to fire a second time as you described.

@fawzialrifai
Copy link
Author

fawzialrifai commented Sep 3, 2021

@IanWyszynski I still get the same output in version 8.6.1 can you please download my file and run it? it's just 7 lines.
https://github.com/fawzialrifai/People

@firebase firebase deleted a comment from fawzialrifai Sep 7, 2021
@firebase firebase deleted a comment from fawzialrifai Sep 7, 2021
paulb777 added a commit that referenced this issue Sep 21, 2021
@fawzialrifai
Copy link
Author

@IanWyszynski @schmidt-sebastian @jeremydurham so this issue is still not fixed after 10 months?

@jmwski
Copy link
Contributor

jmwski commented Apr 18, 2022

@IanWyszynski @schmidt-sebastian @jeremydurham so this issue is still not fixed after 10 months?

Apologies for the delay on this. I'll have a look at this again today.

@fawzialrifai
Copy link
Author

@IanWyszynski thank you, hope you can fix it.

@jmwski
Copy link
Contributor

jmwski commented Apr 19, 2022

@IanWyszynski thank you, hope you can fix it.

I investigated this issue for a few hours yesterday. The problem here is with the way that getDataWithCompletionBlock interacts with the listener cache. Specifically, it is the applyServerOverwriteAtPath that is causing this problem.

It's easy enough to remove that call and make your code sample work, but getDataWithCompletionBlock was added to address an issue with offline persistence where a single-value event listener optimistically checks cache before reaching out to the server on first startup (see this post). In order to address this problem, get has to properly update local cache.

We'll need to either figure out how to merge get responses into the local cache, or re-implement get to wrap a once-listener, but only fallback to it's results if the server is unresponsive. I'm going to take a stab at implementing the later, but the fix we ultimately decide on will require some discussion, so it might take a few days. I'll update this issue with any developments.

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

4 participants