Hunor Gocz
Hunor Gocz

Reputation: 147

How to detect when observer finished Firebase

Hello I have a Firebase app and I would like to download all my database when the app launches. All this is working fine but I would like to know when the download completed.

    func getAllDevices(completion: @escaping (Bool) -> Void) {
    reference.child("devices").queryOrderedByKey().observe(.childAdded) { (snapshot) in
        if let data = snapshot.value as? [String: Any] {
            self.sync(device: data)
        }
       // completion(true)

    }

I tried with completion handler but it is not good because it gets called every time a new object is downloaded. Is there any way to get notified when it finished downloading?

Upvotes: 0

Views: 1069

Answers (1)

Hunor Gocz
Hunor Gocz

Reputation: 147

I could solve the problem using .value event. I did something like this and it is working fine now.

    func getAllDevices(completion: @escaping (Bool) -> Void) {
    reference.child("devices").queryOrderedByKey().observeSingleEvent(of: .value) { (snapshot) in

        if let devices = snapshot.value as? [String: Any] {
            for data in devices.values {
                self.sync(device: data as! [String : Any])
            }
        }

        completion(true)
    }

Upvotes: 1

Related Questions