Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase ios sdk async and threads

Tags:

ios

firebase

Does anyone know whether remote calls to Firebase using Firebase iOS SDK use main thread or a background thread?

I couldn't find anything in documentation.

like image 212
Shadowfax Avatar asked Nov 18 '15 18:11

Shadowfax


2 Answers

Firebase does not entirely eliminate the need to worry about threading. Because Firebase callbacks always return on the main thread, you need to be especially careful when calling Firebase observe/ observerSingleEvent from the background thread.

Consider the use case:

DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated).async {[weak weakSelf = self] in
 // Do something computationally intensive

 ref.child(somePath).observeSingleEvent(of: .value, with: { (snapshot) in
  DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated).async {[weak weakSelf = self] in {
   // Do some computationally intensive non-critical activity based on data retrieved from Firebase
  }
 }
}
like image 91
Raghav Bhagat Avatar answered Oct 05 '22 10:10

Raghav Bhagat


Firebase uses it's own background thread. But as Jay mentioned in the comments, Firebase eliminates the need to worry about threading.

So essentially there's no need to run Firebase in your own background thread.

like image 21
David East Avatar answered Oct 05 '22 10:10

David East