javascript - 当应用程序最小化/关闭时更新 Firestore 值

标签 javascript java firebase google-cloud-firestore google-cloud-functions

尝试查找 these Background Processing methods 中的哪一个适合我的情况。

案例:当应用程序在后台运行或关闭时,如何更新 Firestore 上的字段?

简短描述:用户输入时间(毫秒),当该时间结束时,我想更新 Firestore 中的字段,无论应用程序正在运行还是关闭,我当前有以下代码:

 private void setTaskEndTime(long xEndTime) {
        long mLeftTime = xEndTime - mCurrentTime;

        new CountDownTimer(mLeftTime, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mTimeLeftInMillis = millisUntilFinished;
                updateCountDownText();
            }

            @Override
            public void onFinish() {
                //HERE <===================

            }
        }.start();

    }

我尝试过 Firebase Cloud Function;因此,当时间结束时,应该更新字段,但问题是云函数在更新字段(或至少是我的代码)之前花费了太多时间:

const admin = require('firebase-admin');
const functions = require('firebase-functions');
admin.initializeApp(functions.config().firebase);
let db = admin.firestore();

exports.wtf = functions.region('europe-west1').firestore
    .document('Users/{userId}/Tasks/{docId}')
    .onUpdate((docSnapshot, context) => {
        var mUserID = context.params.userId;  //Document userID
        var mDocID = context.params.docId;    //Document taskID

        var oldSnapshot = docSnapshot.before.data();
        var oldCurrentStatus = oldSnapshot.taskCurrentStatus;
        var newSnapshot = docSnapshot.after.data();
        var newCurrentStatus = newSnapshot.taskCurrentStatus;

        var tempTime = newSnapshot.temporaryTime;

        let cityRef = db.collection('Users').doc(mUserID).collection('Tasks').doc(mDocID);

        if (newCurrentStatus !== oldCurrentStatus && newCurrentStatus > 0) {


            const myFunc = () => {
                //Gets dd/MM/yyyy date and stores it in 'today'
                var today = new Date();
                var dd = String(today.getDate()).padStart(2, '0');
                var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
                var yyyy = today.getFullYear();
                today = dd + '/' + mm + '/' + yyyy;

                let days = cityRef.update({
                    taskCompletedDays: admin.firestore.FieldValue.arrayUnion(today)
                });
                let hours = cityRef.update({
                    taskCompletedHours: admin.firestore.FieldValue.increment(tempTime)
                });
                let status = cityRef.update({ taskCurrentStatus: 0 });
            }

            setTimeout(myFunc, tempTime);

        }
        return true;
    });

最佳答案

不保证使用 setTimeout 安排的代码在 Cloud Functions 中正确执行。后台 Cloud Function(包括 Firestore 触发器)must return a promise that resolves when all of the asynchronous work is complete 。此后,任何待处理的工作都可能被取消或根本无法正确执行。简而言之,不要依赖 setTimeout ,除非您 1) 愿意返回一个仅在工作完成后才解决的 promise ,并且 2) 永远不会超过可配置的最大 9 分钟超时云函数。

您应该做的是使用 Google Cloud Tasks 安排在经过一段时间后将来调用另一个 (HTTP) 函数。您可以在this blog post中阅读详细说明。 .

关于javascript - 当应用程序最小化/关闭时更新 Firestore 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60270394/

相关文章:

java - 无法在Raspberry Pi上加载JNI相关应用程序

javascript - 在 javascript 中向图像添加标记?

javascript - jquery selectbox css - 尝试更改字体颜色无法按预期工作

java - 随着 map 的移动在谷歌地图上获取标记

java - 使用 PopupWindow 的 showAtLocation(View, int, int, int) 时遇到问题

firebase - 如何显示所有用户的倒数时间

java - 读取 firebase DB 后如何更改 ListView 上的颜色和字体

javascript - 为什么我的函数在不应该被调用的时候被调用了?

javascript - Javascript 中的守护线程

javascript - 无法将数字设置为文档路径 Firestore