Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit debf9cf

Browse files
collinjacksonChris Yang
andauthored
[firebase_database] Move firebase_database transaction calls to UI thread on Android (#1812)
* Move calls to UI thread * Update packages/firebase_database/CHANGELOG.md Co-Authored-By: Chris Yang <ychris@google.com>
1 parent 1850be0 commit debf9cf

3 files changed

Lines changed: 51 additions & 34 deletions

File tree

packages/firebase_database/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## 3.0.4
22

3-
* Fix transactions on
3+
* Updated transactions implementation on Android for compatibility with
4+
newer versions of Flutter engine that require channel calls be made
5+
on the UI thread.
46

57
## 3.0.3
68

packages/firebase_database/android/src/main/java/io/flutter/plugins/firebase/database/FirebaseDatabasePlugin.java

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package io.flutter.plugins.firebase.database;
66

7+
import android.app.Activity;
78
import android.util.Log;
89
import android.util.SparseArray;
910
import com.google.android.gms.tasks.Task;
@@ -37,6 +38,7 @@ public class FirebaseDatabasePlugin implements MethodCallHandler {
3738
private static final String TAG = "FirebaseDatabasePlugin";
3839

3940
private final MethodChannel channel;
41+
private final Activity activity;
4042
private static final String EVENT_TYPE_CHILD_ADDED = "_EventType.childAdded";
4143
private static final String EVENT_TYPE_CHILD_REMOVED = "_EventType.childRemoved";
4244
private static final String EVENT_TYPE_CHILD_CHANGED = "_EventType.childChanged";
@@ -50,11 +52,12 @@ public class FirebaseDatabasePlugin implements MethodCallHandler {
5052
public static void registerWith(PluginRegistry.Registrar registrar) {
5153
final MethodChannel channel =
5254
new MethodChannel(registrar.messenger(), "plugins.flutter.io/firebase_database");
53-
channel.setMethodCallHandler(new FirebaseDatabasePlugin(channel));
55+
channel.setMethodCallHandler(new FirebaseDatabasePlugin(channel, registrar.activity()));
5456
}
5557

56-
private FirebaseDatabasePlugin(MethodChannel channel) {
58+
private FirebaseDatabasePlugin(MethodChannel channel, Activity activity) {
5759
this.channel = channel;
60+
this.activity = activity;
5861
}
5962

6063
private DatabaseReference getReference(FirebaseDatabase database, Map<String, Object> arguments) {
@@ -333,7 +336,7 @@ public Transaction.Result doTransaction(MutableData mutableData) {
333336
final Task<Map<String, Object>> updateMutableDataTCSTask =
334337
updateMutableDataTCS.getTask();
335338

336-
Map<String, Object> doTransactionMap = new HashMap<>();
339+
final Map<String, Object> doTransactionMap = new HashMap<>();
337340
doTransactionMap.put("transactionKey", arguments.get("transactionKey"));
338341

339342
final Map<String, Object> snapshotMap = new HashMap<>();
@@ -342,39 +345,46 @@ public Transaction.Result doTransaction(MutableData mutableData) {
342345
doTransactionMap.put("snapshot", snapshotMap);
343346

344347
// Return snapshot to Dart side for update.
345-
channel.invokeMethod(
346-
"DoTransaction",
347-
doTransactionMap,
348-
new MethodChannel.Result() {
348+
activity.runOnUiThread(
349+
new Runnable() {
349350
@Override
350-
@SuppressWarnings("unchecked")
351-
public void success(Object result) {
352-
updateMutableDataTCS.setResult((Map<String, Object>) result);
353-
}
354-
355-
@Override
356-
public void error(
357-
String errorCode, String errorMessage, Object errorDetails) {
358-
String exceptionMessage =
359-
"Error code: "
360-
+ errorCode
361-
+ "\nError message: "
362-
+ errorMessage
363-
+ "\nError details: "
364-
+ errorDetails;
365-
updateMutableDataTCS.setException(new Exception(exceptionMessage));
366-
}
367-
368-
@Override
369-
public void notImplemented() {
370-
updateMutableDataTCS.setException(
371-
new Exception("DoTransaction not implemented on Dart side."));
351+
public void run() {
352+
channel.invokeMethod(
353+
"DoTransaction",
354+
doTransactionMap,
355+
new MethodChannel.Result() {
356+
@Override
357+
@SuppressWarnings("unchecked")
358+
public void success(Object result) {
359+
updateMutableDataTCS.setResult((Map<String, Object>) result);
360+
}
361+
362+
@Override
363+
public void error(
364+
String errorCode, String errorMessage, Object errorDetails) {
365+
String exceptionMessage =
366+
"Error code: "
367+
+ errorCode
368+
+ "\nError message: "
369+
+ errorMessage
370+
+ "\nError details: "
371+
+ errorDetails;
372+
updateMutableDataTCS.setException(
373+
new Exception(exceptionMessage));
374+
}
375+
376+
@Override
377+
public void notImplemented() {
378+
updateMutableDataTCS.setException(
379+
new Exception("DoTransaction not implemented on Dart side."));
380+
}
381+
});
372382
}
373383
});
374384

375385
try {
376386
// Wait for updated snapshot from the Dart side.
377-
Map<String, Object> updatedSnapshotMap =
387+
final Map<String, Object> updatedSnapshotMap =
378388
Tasks.await(
379389
updateMutableDataTCSTask,
380390
(int) arguments.get("transactionTimeout"),
@@ -394,7 +404,7 @@ public void notImplemented() {
394404
@Override
395405
public void onComplete(
396406
DatabaseError databaseError, boolean committed, DataSnapshot dataSnapshot) {
397-
Map<String, Object> completionMap = new HashMap<>();
407+
final Map<String, Object> completionMap = new HashMap<>();
398408
completionMap.put("transactionKey", arguments.get("transactionKey"));
399409
if (databaseError != null) {
400410
completionMap.put("error", asMap(databaseError));
@@ -408,7 +418,12 @@ public void onComplete(
408418
}
409419

410420
// Invoke transaction completion on the Dart side.
411-
result.success(completionMap);
421+
activity.runOnUiThread(
422+
new Runnable() {
423+
public void run() {
424+
result.success(completionMap);
425+
}
426+
});
412427
}
413428
});
414429
break;

packages/firebase_database/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for Firebase Database, a cloud-hosted NoSQL database
33
with realtime data syncing across Android and iOS clients, and offline access.
44
author: Flutter Team <flutter-dev@googlegroups.com>
55
homepage: https://github.com/flutter/plugins/tree/master/packages/firebase_database
6-
version: 3.0.3
6+
version: 3.0.4
77

88
flutter:
99
plugin:

0 commit comments

Comments
 (0)