Reputation: 2840
I know the onDataChange()
in Firebase for let say addListenerForSingleValueEvent
is running on Android Main Ui thread.
I Wanted to understand what will happen if I run this code inside the ExecutorService
:
runInBackground(new Runnable() {
@Override
public void run() {
ref.child("SOME_KEY")
.addListenerForSingleValueEvent
(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
//.. do some work on Ui Thread or not??
}
@Override
public void onCancelled(DatabaseError databaseError) {
notificationRunning = false;
}
}
);
}
});
}
Will the onDataChange() still run on the Main Ui Thread in Android?
And here´s the ExecutorService
runner
/**
* Submits request to be executed in background.
* Threads submitted will be executed synchronously.
*
* @param runnable
*/
private void runInBackground(final Runnable runnable) {
mBackgroundExecutor.submit(new Runnable() {
@Override
public void run() {
try {
runnable.run();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
And the ExecutorService
mBackgroundExecutor = Executors
.newSingleThreadExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
Thread thread = new Thread(runnable,
"Background executor service for OneSignal");
thread.setPriority(Thread.MIN_PRIORITY);
thread.setDaemon(true);
return thread;
}
});
Upvotes: 0
Views: 500
Reputation: 38289
The listener callbacks always run on the main/UI thread. The thread that was used to add the listener does not affect the callback thread. You can confirm this by adding a log statement to your callback that outputs the thread name:
Log.d(TAG, "onDataChange: Thread=" + Thread.currentThread().getName());
Upvotes: 1