Maha Daoud
Maha Daoud

Reputation: 33

Firebase: How to check query is finished? (swift)

I'm trying to display spinner while querying data from firebase, and hide it when the query result is returned. is there any way to know if the query has finished retrieving all data? ?

self.handle = self.ref.child("users").observeEventType(.Value, withBlock: { (snapshot) in
        if snapshot.exists() {
            print ("snapshot exist")
            print (snapshot.childrenCount)
        }
        else {
                print ("snapshot doesn't exist")
            }
        })

Upvotes: 2

Views: 1539

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

When the callback block of a .Value observer is invoked, it gets all data that is currently known for that location. So you can hide the spinner in the block:

self.handle = self.ref.child("users").observeEventType(.Value, withBlock: { (snapshot) in
    // TODO: hide spinner here
    if snapshot.exists() {
        print ("snapshot exist")
        print (snapshot.childrenCount)
    }
    else {
        print ("snapshot doesn't exist")
    }
})

Keep in mind though that a Firebase observer keeps synchronizing data, so you block may be run multiple times.

Upvotes: 2

Related Questions