简体   繁体   中英

when to use firebase functions and when to use firestore client functions?

I have a project where i need to Write a Data along with an image ie to both Cloud Storage and Firestore Database , there is no batch write to both Cloud Storage and Cloud Firestore combined , so the only solution is to do one after the other ie write one thing to database and when it is successful then write the next.

Problem is when writing the first data is succesfull and second data is a failure, then i have to revert the transaction , i think doing this from client side is not good there can be loss of internet connection .

So my question is should i use Cloud Functions for everything i write to database or not ?.

Cloud Functions are essentially small Node scripts that use the Admin SDK to access Firebase. They have no special powers beyond the API, which means they have the same limitations as using that API in other places.

Using a Cloud Function will reduce the chances of having an interruption between the related operations, but it does not eliminate that chance.

This means that you'll have to deal with the interruptions in some way. Typically this is a two-step process:

  1. Ensure that all code reading the data is robust against having incomplete data. For example, after reading the (download) URL from the database, don't assume that the file it points to exists. It might not exist for many reasons (deleted afterwards, service unavailable, etc), so this is a good idea anyway.
  2. Periodically run a script that detects incomplete data and cleans it up. For example, run a Cloud Function every day that gets a list of all files in Cloud Storage, and removes them if there are no references to them in the database, and that then reads all URLs from the database, and checks if they still exist.

I'll admit that the second step is often something I add later. The first step already ensures the apps run fine anyway, so the cleanup is just some data storage optimization at that point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM