2

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")
        }
    }
ktakehi
  • 249
  • 1
  • 3
  • 13

1 Answers1

3

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:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    @ktakehi To add a small bit of info. `func C` is not called either. e.g. when the listener event fires, `print('C')` will not be called - only the code with `...` will be called. The only time C will print is when `func C` is called. – Jay Apr 24 '22 at 14:25