繁体   English   中英

使用本机反应更新 firebase 实时数据库中的 2 个子项

[英]Update 2 children in firebase realtime database with react native

我试图更新我的数据库中的两个孩子(使用实时数据库 firebase),但是当数据库更新时,我的应用程序 go 无缘无故回到主屏幕。

当我仅在“任务”中更新时它可以工作(应用程序不会 go 返回主屏幕)但是当我在“任务”和“用户”中结合更新时会出现这个问题..也许我做得不好..有任何想法吗?

statusPlayback = async (status) => {
    const { navigation } = this.props
    const task = navigation.getParam('task')
    //console.log("task = ", task);



    //to check if we arrived to the end of the
    if (status.didJustFinish) {

        const CountVideoRef = firebase
            .database()
            .ref("Tasks")
            .child(firebase.auth().currentUser.uid).child(task.AssignPerson)
            .child(task.taskname);

        CountVideoRef.once("value").then((snapshot) => {
            CountVideoRef.update({
                countViewVideo: snapshot.val().countViewVideo + 1,
            });
           
        })

       const PointEndVideoRef = firebase
                .database()
                .ref("Users")
                .child(firebase.auth().currentUser.uid);

            PointEndVideoRef.once("value").then((snapshot1) => {
                PointEndVideoRef.update({
                    Points: snapshot1.val().Points + 10,
                });

                const points = (snapshot1.val().Points) + 10
                //console.log("points = ", points)
                this.props.updatePoints({ points: points })
            })




        this.setState({ showButtonVisible: true });
    }
};

我怀疑这是导航问题的原因,但这种更新方式充满了问题:

CountVideoRef.once("value").then((snapshot) => {
    CountVideoRef.update({
        countViewVideo: snapshot.val().countViewVideo + 1,
    });
   
})

如果您需要根据现有值更新数据库中的现有值,则应该使用事务来防止多个用户在同一时间(或离线时)执行相同操作时出现竞争条件。

在这种情况下,您可以使用更简单的原子服务器端增量操作,这意味着上面变成:

CountVideoRef.set(firebase.database.ServerValue.increment(1));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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