Skip to content

[cloud_firestore] Fixes crash on Android when run a transaction #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/cloud_firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.12.9+5

* Fixes a crash on Android when running a transaction without an internet connection.

## 0.12.9+4

* Fix integer conversion warnings on iOS.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,10 @@ public void onMethodCall(MethodCall call, final Result result) {
final Map<String, Object> arguments = call.arguments();
getFirestore(arguments)
.runTransaction(
new Transaction.Function<Map<String, Object>>() {
new Transaction.Function<TransactionResult>() {
@Nullable
@Override
public Map<String, Object> apply(@NonNull Transaction transaction) {
public TransactionResult apply(@NonNull Transaction transaction) {
// Store transaction.
int transactionId = (Integer) arguments.get("transactionId");
transactions.append(transactionId, transaction);
Expand Down Expand Up @@ -424,23 +424,31 @@ public void notImplemented() {
Tasks.await(transactionTCSTask, timeout, TimeUnit.MILLISECONDS);

// Once transaction completes return the result to the Dart side.
return transactionResult;
return new TransactionResult(transactionResult);
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
result.error("Error performing transaction", e.getMessage(), null);
return new TransactionResult(e);
}
return null;
}
})
.addOnCompleteListener(
new OnCompleteListener<Map<String, Object>>() {
new OnCompleteListener<TransactionResult>() {
@Override
public void onComplete(Task<Map<String, Object>> task) {
if (task.isSuccessful()) {
result.success(task.getResult());
} else {
public void onComplete(Task<TransactionResult> task) {
if (!task.isSuccessful()) {
result.error(
"Error performing transaction", task.getException().getMessage(), null);
return;
}

TransactionResult transactionResult = task.getResult();
if (transactionResult.exception == null) {
result.success(transactionResult.result);
} else {
result.error(
"Error performing transaction",
transactionResult.exception.getMessage(),
null);
}
}
});
Expand Down Expand Up @@ -820,6 +828,21 @@ public void onFailure(@NonNull Exception e) {
}
}
}

private static final class TransactionResult {
final @Nullable Map<String, Object> result;
final @Nullable Exception exception;

TransactionResult(@NonNull Exception exception) {
this.exception = exception;
this.result = null;
}

TransactionResult(@Nullable Map<String, Object> result) {
this.result = result;
this.exception = null;
}
}
}

final class FirestoreMessageCodec extends StandardMessageCodec {
Expand Down