Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know if a transaction on Firestore succeded?

Is there any method to know if the transaction was successful? I need to implement a loading "widget animation" if I upload large archives and spend too much time on that. And then change the screen after success, but I don't know-how.

Thanks!

Transaction Example:

CollectionReference reference = Firestore.instance.collection("collection_example");

Firestore.instance.runTransaction((Transaction transaction) async {

await transaction.set(reference, {
  "index_1":"ABC",
  "index_2": 2,
  "index_3": {"mapIndex_1": "ABC"}
});

});
like image 402
Eduardo Yamauchi Avatar asked Jun 16 '18 14:06

Eduardo Yamauchi


People also ask

How do you check data on firestore?

Cloud Firestore doesn't support native indexing or search for text fields in documents. Additionally, downloading an entire collection to search for fields client-side isn't practical. To enable full text search of your Cloud Firestore data, use a dedicated third-party search service.

What counts as a firestore read?

When you listen to the results of a query, you are charged for a read each time a document in the result set is added or updated. You are also charged for a read when a document is removed from the result set because the document has changed. (In contrast, when a document is deleted, you are not charged for a read.)

What is a firestore transaction?

There are two types of atomic operations in Cloud Firestore: Transactions: a transaction is a set of read and write operations on one or more documents. Batched Writes: a batched write is a set of write operations on one or more documents.

Is firestore real time?

Firebase offers two cloud-based, client-accessible database solutions that support realtime data syncing: Cloud Firestore is Firebase's newest database for mobile app development. It builds on the successes of the Realtime Database with a new, more intuitive data model.


1 Answers

You cannot receive that from your runTransaction call in this case because it returns a Future<Map<String, dynamic>>. As I have checked, it will always return an empty Map. Thus there is nothing to get from the runTransaction function itself.

You can easily get a Stream of updates from your reference though, which would look something like this:

DocumentReference documentReference;

firestore.runTransaction( // firestore in this case is your Firestore instance
  (Transaction transaction) async {
    // whatever you do here with your reference
    await transaction.update(
      documentReference,
      ...
);

documentReference.snapshots().listen((DocumentSnapshot event) {
  // here you could e.g. check if the transaction on your reference was succesful
});

As you can see I used the snapshots() Stream<DocumentSnapshot) on the same DocumentReference as the transaction was run on. The Stream will update as soon as the transaction has completed and the server reached back to you.

To see why transaction results cannot be evaluated client-side check the answers on my question here.

like image 179
creativecreatorormaybenot Avatar answered Oct 09 '22 19:10

creativecreatorormaybenot