1

I have searched online but haven't found some answer related with my question. Hope someone could help me with this. Thanks in advance! Here is a Firebase observeEvent function.

ref.child("path").observeSingleEvent(of: .value, with: { (snapshot) in 
// here I have a for loop
for (_, dict) snapshot.value as! NSDictionary {
    // do something...
    //
    // here I would like to do some UI updating, like a progress bar, or just as simple as update the text in a label
    label.text = "an object fetched."
}
// I used to do some UI update here, and it works, like tableview.reloadData(), but this time I would like the UI updating happen in that for loop

}) { (error) in
    print(error.localizedDescription)
}

Could someone help me with this? I tried

DispatchQueue.main.async {
 // UI update here but it doesn't work
}

Hope someone could help me, thanks a lot!

  • Could someone help me with this? Thanks. I would like to implement progress bar animation with Firebase database. – Michael GUO Mar 20 '17 at 20:03
  • Found a working solution [here](http://stackoverflow.com/questions/6835472/uilabel-text-not-being-updated) Check it out. – Michael GUO Mar 21 '17 at 16:31

1 Answers1

0

Firebase event callbacks fire on the main/UI thread. You don't need to go through a dispatch queue, but can simply update the UI from within the callback.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • but I also tried update the UI directly like "label.text = "blablabla" ", but it didn't work. – Michael GUO Mar 20 '17 at 17:11
  • if I do some UI update after the for loop, it will work. But now what I would like to implement is something like a progress bar, let's say if it will loop for 100 times, so every single loop, I would like to update the UI, but it didn't work within the for loop – Michael GUO Mar 20 '17 at 17:14