在Firebase中存储ID列表 [英] Storing a list of ids in Firebase

查看:38
本文介绍了在Firebase中存储ID列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何使用firebase解决此问题: 我有用户,每个用户都有帖子,每个帖子都有一个由Firebase生成的ID,如何将这些ID存储在用户节点中?

I can't figure out how to solve this issue with firebase : I have users, each user has posts, each post has an id generated by firebase, how can I store these ids in a user node ?

我正在使用字符串,将它们连接起来,然后在我的js应用程序中对其进行解析.基本上将它们视为csv文件.但这是一个非常丑陋的解决方案

I'm using string,concatenating them, parsing them in my js app. Basically treating them as a csv file. But i guess that's a very ugly solution

存储这种数据的方式是什么?

What would be the way to store this kind of data ?

UserID:

  • 用户名=用户名"
  • 帖子="id1,id2,id3,id4"

当用户有新帖子时,我使用事务在字符串末尾附加新ID.当我需要删除ID时,我再次使用事务,并使用以下代码删除元素:

When a user has a new post, I use a transaction to append a new id at the end of the string. When I need to delete an id, again I use a transaction and I delete the element using this code:

removeElem(list, value) {
var separator = ",";
var values = list.split(separator);
for (var i = 0; i < values.length; i++) {
    if (values[i] == value) {
        values.splice(i, 1);
        return values.join(separator);
    }
}
return list;
},

推荐答案

虽然事务可以做到这一点,但它严重损害了可伸缩性,并且当用户暂时失去连接时根本无法工作.为了获得更好的解决方案,请摆脱阵列逻辑并使用Firebase的push()方法.来自有关保存数据列表的Firebase文档:

While transactions will work for this, it seriously hurts scalability and will not work at all when the user temporarily loses connectivity. For a better solution, get rid of the array logic and use Firebase's push() method. From the Firebase documentation on saving lists of data:

推送与交易

在处理数据列表时,push()确保唯一且按时间顺序排列的ID.您可能很想使用事务来生成自己的ID,但是推送是一个更好的选择.交易更慢,更复杂.他们需要一次或多次往返服务器.可以在客户端上生成推送ID,该ID可以在脱机时使用,并且针对性能进行了优化.

When working with lists of data push() ensures a unique and chronological ID. You may be tempted to use transactions instead to generate your own IDs, but push is a far better choice. Transactions are slower and more complex. They require one or more round trips to the server. A push ID can be generated on the client will work while offline and is optimized for performance.

虽然可能需要一些时间来适应非顺序键,但从长远来看,它会变得更好.

While it may take some getting used to the non-sequential keys, it's going to be better in the long run.

这篇关于在Firebase中存储ID列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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