Home > Net >  addSnapShotListener triggers all the function dependent to it?
addSnapShotListener triggers all the function dependent to it?

Time:04-23

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")
        }
    }

CodePudding user response:

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:

  • Related