Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
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
7 changes: 7 additions & 0 deletions packages/cloud_firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.12.7

* Methods of `Transaction` no longer require `await`.
* Added documentation to methods of `Transaction`.
* Removed an unnecessary log on Android.
* Added an integration test for rapidly incrementing field value.

## 0.12.6

* Support for `orderBy` on map fields (e.g. `orderBy('cake.flavor')`) for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public void error(
String errorMessage,
Object errorDetails) {
transactionTCS.trySetException(
new Exception("Do transaction failed."));
new Exception("DoTransaction failed: " + errorMessage));
}

@Override
Expand Down Expand Up @@ -528,7 +528,6 @@ protected Void doInBackground(Void... voids) {
new Runnable() {
@Override
public void run() {
Log.d(TAG, "sending set success");
result.success(null);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ void main() {
});
snapshot = await ref.get();
expect(snapshot.data['message'], 42.1);

// Call several times without awaiting the result
await Future.wait<void>(List<Future<void>>.generate(
3,
(int i) => ref.updateData(<String, dynamic>{
'message': FieldValue.increment(i),
}),
));
snapshot = await ref.get();
expect(snapshot.data['message'], 45.1);
await ref.delete();
});

Expand All @@ -107,11 +117,12 @@ void main() {
final Map<String, dynamic> updatedData =
Map<String, dynamic>.from(snapshot.data);
updatedData['message'] = 'testing2';
await tx.update(ref, updatedData);
tx.update(ref, updatedData); // calling await here is optional
return updatedData;
},
);
expect(result['message'], 'testing2');

await ref.delete();
final DocumentSnapshot nonexistentSnapshot = await ref.get();
expect(nonexistentSnapshot.data, null);
Expand Down
8 changes: 5 additions & 3 deletions packages/cloud_firestore/lib/src/firestore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ class Firestore {
_documentObservers[call.arguments['handle']].add(snapshot);
} else if (call.method == 'DoTransaction') {
final int transactionId = call.arguments['transactionId'];
return _transactionHandlers[transactionId](
Transaction(transactionId, this),
);
final Transaction transaction = Transaction(transactionId, this);
final dynamic result =
await _transactionHandlers[transactionId](transaction);
await transaction._finish();
return result;
}
});
_initialized = true;
Expand Down
48 changes: 46 additions & 2 deletions packages/cloud_firestore/lib/src/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@ class Transaction {

int _transactionId;
Firestore _firestore;
List<Future<dynamic>> _pendingResults = <Future<dynamic>>[];
Future<void> _finish() => Future.wait<void>(_pendingResults);

Future<DocumentSnapshot> get(DocumentReference documentReference) async {
/// Reads the document referenced by the provided DocumentReference.
Future<DocumentSnapshot> get(DocumentReference documentReference) {
final Future<DocumentSnapshot> result = _get(documentReference);
_pendingResults.add(result);
return result;
}

Future<DocumentSnapshot> _get(DocumentReference documentReference) async {
final Map<String, dynamic> result = await Firestore.channel
.invokeMapMethod<String, dynamic>('Transaction#get', <String, dynamic>{
'app': _firestore.app.name,
Expand All @@ -32,7 +41,17 @@ class Transaction {
}
}

Future<void> delete(DocumentReference documentReference) async {
/// Deletes the document referred to by the provided [documentReference].
///
/// Awaiting the returned [Future] is optional and will be done automatically
/// when the transaction handler completes.
Future<void> delete(DocumentReference documentReference) {
final Future<void> result = _delete(documentReference);
_pendingResults.add(result);
return result;
}

Future<void> _delete(DocumentReference documentReference) async {
return Firestore.channel
.invokeMethod<void>('Transaction#delete', <String, dynamic>{
'app': _firestore.app.name,
Expand All @@ -41,8 +60,20 @@ class Transaction {
});
}

/// Updates fields in the document referred to by [documentReference].
/// The update will fail if applied to a document that does not exist.
///
/// Awaiting the returned [Future] is optional and will be done automatically
/// when the transaction handler completes.
Future<void> update(
DocumentReference documentReference, Map<String, dynamic> data) async {
final Future<void> result = _update(documentReference, data);
_pendingResults.add(result);
return result;
}

Future<void> _update(
DocumentReference documentReference, Map<String, dynamic> data) async {
return Firestore.channel
.invokeMethod<void>('Transaction#update', <String, dynamic>{
'app': _firestore.app.name,
Expand All @@ -52,7 +83,20 @@ class Transaction {
});
}

/// Writes to the document referred to by the provided [DocumentReference].
/// If the document does not exist yet, it will be created. If you pass
/// SetOptions, the provided data can be merged into the existing document.
///
/// Awaiting the returned [Future] is optional and will be done automatically
/// when the transaction handler completes.
Future<void> set(
DocumentReference documentReference, Map<String, dynamic> data) {
final Future<void> result = _set(documentReference, data);
_pendingResults.add(result);
return result;
}

Future<void> _set(
DocumentReference documentReference, Map<String, dynamic> data) async {
return Firestore.channel
.invokeMethod<void>('Transaction#set', <String, dynamic>{
Expand Down
2 changes: 1 addition & 1 deletion packages/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
live synchronization and offline support on Android and iOS.
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/cloud_firestore
version: 0.12.6
version: 0.12.7

flutter:
plugin:
Expand Down
4 changes: 4 additions & 0 deletions packages/firebase_database/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.0.4

* Fix transactions on

## 3.0.3

* Automatically use version from pubspec.yaml when reporting usage to Firebase.
Expand Down