How to create auto incremented key in Firebase?(如何在 Firebase 中创建自动递增的密钥?)
问题描述
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
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?
I do know we have
push()
and
childByAutoId()
but this does not fit for my problem. I am working with Swift iOS just.
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.
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.
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.
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
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 中创建自动递增的密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Firebase 中创建自动递增的密钥?


基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01