简体   繁体   English

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

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

Im trying to update two children in my database (using realtime database firebase), but when database is updated, my application go back to the home screen for no reason.我试图更新我的数据库中的两个孩子(使用实时数据库 firebase),但是当数据库更新时,我的应用程序 go 无缘无故回到主屏幕。

When I update only in "Tasks" it works (the app doesnt go back to the home screen) but when I combine update in "Tasks" and "Users" there is this problem.. Maybe i dont do it the good way.. Any ideas?当我仅在“任务”中更新时它可以工作(应用程序不会 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 });
    }
};

I doubt this is the cause of the navigation problem, but this style of updating is fraught with problems:我怀疑这是导航问题的原因,但这种更新方式充满了问题:

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

If you need to update an existing value in the database based on its existing value, you should use a transaction to prevent race conditions when multiple users perform the same action around the same time (or while offline).如果您需要根据现有值更新数据库中的现有值,则应该使用事务来防止多个用户在同一时间(或离线时)执行相同操作时出现竞争条件。

In this case, you can use the simpler atomic server-side increment operation, which means the above becomes:在这种情况下,您可以使用更简单的原子服务器端增量操作,这意味着上面变成:

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

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

相关问题 使用 react native 在实时数据库 firebase 中检索孩子的孩子 - Retrieve children of child in realtime database firebase with react native 如何从 Firebase 实时数据库中获取数据,这些数据是随机密钥的子项 - 使用本机反应? - How do I fetch data from firebase realtime database which are children to a random key - using react native? 将 Firebase 实时数据库与 React Native 应用程序集成 - Integrating Firebase Realtime Database with React Native App 在Firebase中更新实时数据库 - Update realtime database in Firebase 如何使用Firebase Realtime数据库和React Native上传图像/文件? - How to upload an image / file using Firebase Realtime database and React Native? 无法从 React Native 中的 Firebase 实时数据库读取数据 - Unable to read data from Firebase Realtime Database in React Native 如何在 React Native 中将 Firebase 实时数据库数据值加一? - How to increment Firebase Realtime database data values by one in React Native? 如何在反应原生中修改 firebase 实时数据库中的数组值 - How to modify array values in firebase realtime database in react native Firebase 实时数据库订单按孩子与大多数孩子 - Firebase realtime database order by child with most children 如何使用 Firebase 实时数据库查询所有孩子 - How to query all children with Firebase Realtime Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM