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 中创建自动递增的密钥?


基础教程推荐
- iOS4 创建后台定时器 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01