[英]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.