@@ -45,14 +45,17 @@ private interface Callback {
4545
4646 public static final String TAG = "FirebaseCb" ;
4747
48+ // Object used to synchronize around. All synchronization should be done with this,
49+ // as there isn't a guarantee on which function will be called first.
50+ private final Object lockObject = new Object ();
51+
4852 /**
4953 * This class is registered with a Task object as an OnSuccessListener, OnFailureListener, and
5054 * OnCanceledListener, redirecting completion status to the C++ method nativeOnResult.
5155 */
5256 private class TaskCallback <TResult >
5357 implements OnSuccessListener <TResult >, OnFailureListener , OnCanceledListener , Callback {
5458 private Task <TResult > task ;
55- private final Object lockObject = new Object ();
5659 /**
5760 * Register with a Task instance to capture the completion callback.
5861 */
@@ -129,7 +132,7 @@ protected void initializeNativeCallbackFunctionAndData(long callbackFn, long cal
129132
130133 /** Initialize / attach the instance to a pending result or task object. */
131134 protected void initializeWithTask (Task <TResult > task ) {
132- synchronized (this ) {
135+ synchronized (lockObject ) {
133136 callbackHandler = new TaskCallback <TResult >(task );
134137 callbackHandler .register ();
135138 }
@@ -146,7 +149,7 @@ public void cancel() {
146149 /** Call nativeOnResult with the registered callbackFn and callbackData. */
147150 public void onCompletion (
148151 Object result , boolean success , boolean cancelled , String statusMessage ) {
149- synchronized (this ) {
152+ synchronized (lockObject ) {
150153 if (callbackHandler != null ) {
151154 nativeOnResult (result , success , cancelled , statusMessage , callbackFn , callbackData );
152155 callbackHandler .disconnect ();
0 commit comments