【问题标题】:Update Firestore values when App is minimised/closed在应用最小化/关闭时更新 Firestore 值
【发布时间】:2020-06-01 20:30:17
【问题描述】:

试图找出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;
    });

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 我已经查看了一些博客和文档,尽管我认为这对于我的简单新手项目来说有点过头了,但我会尝试相应地实施和更新这个主题(这肯定需要时间因为所有这些术语听起来都太复杂了)。感谢您的帮助!
    • 由于 setTimeout 不起作用,如果您需要精确地安排工作,这实际上是正确执行此操作的最简单的方法。
    猜你喜欢
    • 2018-09-05
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多