I heard that I should always do user interface work on the main thread while reading data from Firebase.
I tried to do self.tableView.reloadData on the background thread inside a Firebase observe(.value) function and the app did not crash or freeze.
databaseReference.observe(.value) { [weak self] snapshot in
guard let self = self else { return }
self.tableView.reloadData()
}
I tried to do the same work inside getData function instead of observe(.value) function and the app crashed.
databaseReference.getData { [weak self] error, snapshot in
guard let self = self else { return }
self.tableView.reloadData()
}
So why the app crashed inside getData function and it did not crash inside observe(.value) function?

firebaser here
While the Firebase SDK performs its network and other I/O on a background thread, it actually calls your callback on the main thread.
But it seems that getData does not do that here, which is a bug. So thanks for catching and reporting that. 🙏
I filed bug #8245 on the Github repo, so check there for progress.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With