Skip to content

Commit

Permalink
[cloud_firestore] Clean up Android Firestore listeners (firebase#2733)
Browse files Browse the repository at this point in the history
* Remove listeners when view is destroyed or when detached from engine.
  • Loading branch information
kroikie authored Jun 19, 2020
1 parent 8ecdf43 commit db92fc3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/cloud_firestore/cloud_firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.13.7

* Clean up snapshot listeners when Android Activity is destroyed.

## 0.13.6

* Update lower bound of dart dependency to 2.0.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.StandardMessageCodec;
import io.flutter.plugin.common.StandardMethodCodec;
import io.flutter.view.FlutterNativeView;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -76,9 +77,17 @@ public class CloudFirestorePlugin implements MethodCallHandler, FlutterPlugin, A
private final SparseArray<TaskCompletionSource> completionTasks = new SparseArray<>();

public static void registerWith(PluginRegistry.Registrar registrar) {
CloudFirestorePlugin instance = new CloudFirestorePlugin();
final CloudFirestorePlugin instance = new CloudFirestorePlugin();
instance.activity = registrar.activity();
instance.initInstance(registrar.messenger());
registrar.addViewDestroyListener(
new PluginRegistry.ViewDestroyListener() {
@Override
public boolean onViewDestroy(FlutterNativeView view) {
instance.onDetachedFromEngine();
return false;
}
});
}

private void initInstance(BinaryMessenger messenger) {
Expand Down Expand Up @@ -476,6 +485,11 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {

@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
onDetachedFromEngine();
}

private void onDetachedFromEngine() {
removeSnapshotListeners();
channel.setMethodCallHandler(null);
channel = null;
}
Expand Down Expand Up @@ -985,6 +999,13 @@ private static final class TransactionResult {
this.exception = null;
}
}

private void removeSnapshotListeners() {
for (int i = 0; i < listenerRegistrations.size(); i++) {
listenerRegistrations.valueAt(i).remove();
}
listenerRegistrations.clear();
}
}

final class FirestoreMessageCodec extends StandardMessageCodec {
Expand Down
2 changes: 1 addition & 1 deletion packages/cloud_firestore/cloud_firestore/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description:
Flutter plugin for Cloud Firestore, a cloud-hosted, noSQL database with
live synchronization and offline support on Android and iOS.
homepage: https://github.com/FirebaseExtended/flutterfire/tree/master/packages/cloud_firestore/cloud_firestore
version: 0.13.6
version: 0.13.7

flutter:
plugin:
Expand Down

0 comments on commit db92fc3

Please sign in to comment.