Firebase Android: onDataChange() event always executed in Main UI Thread?

后端 未结 2 2034
忘了有多久
忘了有多久 2020-12-01 12:34

In an Android application using Firebase, I need to execute long operations in background once Firebase returns a query answer. E.g.:

query.addListenerForSi         


        
2条回答
  •  清歌不尽
    2020-12-01 13:21

    The callbacks for your Firebase listeners are indeed executed on the main UI thread.

    If you need to do heavy operations in a callback, spin up an AsyncTask and do the work there. The AsyncTask.doInBackground() method executes on a different thread, so it won't block the Android UI. But AsyncTask. onPostExecute() executes on the main thread again, so you can update your UI views from there.

    If you're using Cloud Firestore, see Abhriya Roy's answer on how to get the callbacks on a background thread.

提交回复
热议问题