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

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

问题描述

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

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

我想创建一个这样的结构

I was wanting to create a structure like this

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

有这样的结构吗?

我知道我们有

推()

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/.其核心是在数据库中保留一个具有当前最高序列号的 counter 属性,然后使用事务来更新它.

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天全站免登陆