簡體   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