如何从Firebase数据库获取最新数据? [英] How to get the most recent data from firebase database?

查看:158
本文介绍了如何从Firebase数据库获取最新数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我正在用Firebase数据库编写一个Twitter克隆(但要简单得多).帖子仅包含消息正文.在数据库中,除了消息正文之外,我还跟踪这些事情:

Suppose I am writing a Twitter clone (but much simpler) with firebase database. A post contains only the message body. In the database, besides the message body, I also keep track of these things:

其中,likeCount是帖子上的点赞次数.每当有人喜欢该帖子时,它就会增加.帖子通过Cloud Functions下载,因为使用了分页并且一次下载了10个帖子.因此,我的问题是,在下载时,假设每个帖子只有1个赞,但是如果其他人喜欢一个帖子,那么现在该帖子上应该有2个赞,我如何获得此最新的likeCount无需刷新页面?

where likeCount is the number of likes on the post. It's incremented whenever someone likes the post. Posts are downloaded via Cloud Functions, because pagination is used and 10 posts are downloaded at a time. So, my question is, at the time of download, suppose each post has only 1 like, but if someone else likes a post, therefore, there should be 2 likes on the post now, how to I get this most recent likeCount without having to refresh the page?

推荐答案

在Android中,您应该这样做以获得likeCount节点的更新

In Android you should do like this to get the Updates of likeCount node

FirebaseDatabase database;
String postId="KyELxNpEaRt2Y64flUn"

database.getReference().child("posts/" + postId + "/likeCount").onChildChanged(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) 
{
if (dataSnapshot.getValue() != null) {
//here you should update the value
}
})
})

在React Native中

In React Native

 postRef=`/posts/${postId}/likeCount`;
 firebase.database().ref(postRef).on("child_changed", snap => {
    if (snap.val() != null) {
     //here you shold update the value
    }
  });

这篇关于如何从Firebase数据库获取最新数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆