如何在Firebase中创建自动递增的密钥? [英] How to create auto incremented key in Firebase?

查看:142
本文介绍了如何在Firebase中创建自动递增的密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我们是否可以像在SQL中一样在Firebase中创建自动增量ID(键)!

I was wondering if we could create auto increment ID's(Keys) in Firebase like we have in SQL!

我想要创建这样的结构

rules:
  Line1:
    1: Smith
    2: Alex
    3: Hello
  Line2:
    1: Brown
    2: Michael

因此,如果我想将Erik添加到第1行,它应该像这样做

So lets say if I want to add "Erik" to Line 1 it should do it like this

Line1:
    1: Smith
    2: Alex
    3: Hello
    4: Erik

是否有可能有这样的结构?

Is it possible have structure like that?

我知道我们已经


push()

push()


childByAutoId()

childByAutoId()

但这不适合我的问题。我正在使用Swift iOS。

but this does not fit for my problem. I am working with Swift iOS just.

推荐答案

此类顺序密钥固有地限制了多用户分布式系统的可扩展性,该系统还需要处理缺乏连接的问题。这就是Firebase不支持它们的原因之一。

Such sequential keys inherently limit the scalability of a multi-user distributed system that also needs to deal with a lack of connectivity. That's one of the reasons why Firebase doesn't support them.

相反,Firebase有自己的密钥类型,称为推送ID 。就像你正在寻找的序列一样,它们是单调增加的,并且它们也保证是独一无二的。但最大的区别在于推送ID与大多数SQL数据库的序列号不同,可以在客户端确定。

Instead Firebase has its own key type, called push IDs. Like the sequence you're looking for these are monotonously increasing, and they are also guaranteed to be unique. But the big difference is that push IDs, unlike the sequence numbers of most SQL databases, can be determined client side.

有关数组的更多信息(与序列类似) )在Firebase中,请参阅此博文: https:/ /firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html

For more information on arrays (which are similar to sequences) in Firebase, see this blog post: https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html.

有关Firebase推送ID的详细信息,请参阅此博文: https:// firebase .googleblog.com / 2015/02 / the-2120-ways-to-ensure-unique_68.html

For more information on Firebase's push IDs, see this blog post: https://firebase.googleblog.com/2015/02/the-2120-ways-to-ensure-unique_68.html

如果您仍然决定必须使用顺序ID,请看看Kato在此处的实现: http://jsfiddle.net/katowulf/5ESSp/ 。其核心是在数据库中保留一个计数器属性,并使用当前最高序列号,然后使用事务来更新它。

If you still decide that you must use sequential IDs, have a look at Kato's implementation of that here: http://jsfiddle.net/katowulf/5ESSp/. The core of this is keeping a counter property in the database with the current highest sequence number, and then using a transaction to update that.

这篇关于如何在Firebase中创建自动递增的密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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