Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

addSnapShotListener triggers all the function dependent to it?

I understand that when the data changes in FireStore, AddsnapshotListener gets triggered in realtime.
However, I’m not sure which function gets triggered by that, in the below code.
Is it only function C that gets triggered? or all the functions?
If all functions run, what happens to the argument of the function A??

func C() {
        print("C")
        deckDocRef.addSnapshotListener { snapShot, err in
            ...
        }
    }
    
    func B() {
        print("func B")
        C()
    }
    
    func A(a: Bool) {
        if a {
            print("a")
            B()
        } else {
            print("b")
        }
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When the data changed (and at the initial load) only the code inside your listener (the ... in your question) is called. There is no effect on functions A or B. So any code that needs the data from the database, has to be inside the snapshot callback, be called from there, or be otherwise synchronized with that code.

If this is surprising to you, you may be new to dealing with asynchronous callbacks. If that’s the case, I recommend checking out:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading