Navigation Menu

Skip to content

Commit

Permalink
fix(cloud_firestore): Fix Android Firestore transaction crash when ru…
Browse files Browse the repository at this point in the history
…nning in background caused by `null` `Activity`. (#7627)

Co-authored-by: Salakar <mike.diarmid@gmail.com>
Co-authored-by: Russell Wheatley <russellwheatley85@gmail.com>
  • Loading branch information
3 people committed Jan 18, 2022
1 parent 1023f27 commit 8d60d47
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -54,3 +54,4 @@ Akora Ing. DKB <akoraingdkb@gmail.com>
Samay Bhattacharyya <contact@samay.dev>
Kevin McGill <kevin@mcgilldevtech.com>
KikiManjaro <kylian.meulin@gmail.com>
Timur Dyushaliev <timurdyushaliev@gmail.com>
Expand Up @@ -393,7 +393,7 @@ public void onMethodCall(MethodCall call, @NonNull final MethodChannel.Result re
final String transactionId = UUID.randomUUID().toString().toLowerCase(Locale.US);
final TransactionStreamHandler handler =
new TransactionStreamHandler(
activity, transaction -> transactions.put(transactionId, transaction));
transaction -> transactions.put(transactionId, transaction));

registerEventChannel(METHOD_CHANNEL_NAME + "/transaction", transactionId, handler);
transactionHandlers.put(transactionId, handler);
Expand Down
@@ -1,6 +1,7 @@
package io.flutter.plugins.firebase.firestore.streamhandler;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.Nullable;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FieldPath;
Expand All @@ -19,7 +20,6 @@
import java.util.Objects;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

public class TransactionStreamHandler implements OnTransactionResultListener, StreamHandler {

Expand All @@ -28,18 +28,15 @@ public interface OnTransactionStartedListener {
void onStarted(Transaction transaction);
}

final AtomicReference<Activity> activityRef;
final OnTransactionStartedListener onTransactionStartedListener;

public TransactionStreamHandler(
AtomicReference<Activity> activityRef,
OnTransactionStartedListener onTransactionStartedListener) {
this.activityRef = activityRef;
public TransactionStreamHandler(OnTransactionStartedListener onTransactionStartedListener) {
this.onTransactionStartedListener = onTransactionStartedListener;
}

final Semaphore semaphore = new Semaphore(0);
final Map<String, Object> response = new HashMap<>();
final Handler mainLooper = new Handler(Looper.getMainLooper());

@Override
public void onListen(Object arguments, EventSink events) {
Expand Down Expand Up @@ -68,7 +65,7 @@ public void onListen(Object arguments, EventSink events) {
Map<String, Object> attemptMap = new HashMap<>();
attemptMap.put("appName", firestore.getApp().getName());

activityRef.get().runOnUiThread(() -> events.success(attemptMap));
mainLooper.post(() -> events.success(attemptMap));

try {
if (!semaphore.tryAcquire(timeout, TimeUnit.MILLISECONDS)) {
Expand Down Expand Up @@ -149,8 +146,11 @@ public void onListen(Object arguments, EventSink events) {
map.put("complete", true);
}

activityRef.get().runOnUiThread(() -> events.success(map));
activityRef.get().runOnUiThread(events::endOfStream);
mainLooper.post(
() -> {
events.success(map);
events.endOfStream();
});
});
}

Expand Down

0 comments on commit 8d60d47

Please sign in to comment.