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

后端 未结 2 2016
忘了有多久
忘了有多久 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.

    0 讨论(0)
  • 2020-12-01 13:27

    Might be a little too late to the party, but if you want Firebase to return call backs on the background thread, you can use the BACKGROUND_EXECUTOR from com.google.firebase.firestore.util.Executors.BACKGROUND_EXECUTOR like

    .addOnCompleteListener(BACKGROUND_EXECUTOR, OnCompleteListener<Void> {
       ...
    }
    

    There are other executers as well like -

    DEFAULT_CALLBACK_EXECUTOR
    DIRECT_EXECUTOR 
    
    0 讨论(0)
提交回复
热议问题