从 Firebase 实时数据库中获取数据的保证顺序
Guaranteed order of fetched data from Firebase Realtime Database
当我在不使用任何 orderBy()
方法的情况下从我的 Firebase 实时数据库中获取数据时,数据的顺序是什么?
如果我使用 get()
,是否总是 100% 保证我将获得的数据从旧到新排序,或者键是否按字典顺序排列?
What is the order of data when I fetch from my Firebase Realtime Database without using any orderBy methods?
如果您希望在不调用 orderBy()
的情况下对元素进行排序,那么您应该使用 DatabaseReference#push() method. These pushed IDs aren't actually timestamps but they contain a time component. As Michael Lehenbauer mentioned in this blog article:[=18= 提供的推送 ID ]
Push IDs are string identifiers that are generated client-side. They are a combination of a timestamp and some random bits. The timestamp ensures they are ordered chronologically, and the random bits ensure that each ID is unique, even if thousands of people are creating push IDs at the same time.
然后回答你的第二个问题:
If I use the get(), is it always 100% guaranteed that the data I will get is sorted from oldest to the newest or will it be in lexicographical order?
如果加一个ValueEventListener or ChildEventListener or even if you call Query#get()也没关系,结果会按照同样的顺序排列。
但是,如果您需要根据时间组件明确排序,那么您应该添加一个 属性 类型的“timestamp”,正如我在以下 post 中的回答中所解释的:
如果您想颠倒顺序,请检查以下答案:
当我在不使用任何 orderBy()
方法的情况下从我的 Firebase 实时数据库中获取数据时,数据的顺序是什么?
如果我使用 get()
,是否总是 100% 保证我将获得的数据从旧到新排序,或者键是否按字典顺序排列?
What is the order of data when I fetch from my Firebase Realtime Database without using any orderBy methods?
如果您希望在不调用 orderBy()
的情况下对元素进行排序,那么您应该使用 DatabaseReference#push() method. These pushed IDs aren't actually timestamps but they contain a time component. As Michael Lehenbauer mentioned in this blog article:[=18= 提供的推送 ID ]
Push IDs are string identifiers that are generated client-side. They are a combination of a timestamp and some random bits. The timestamp ensures they are ordered chronologically, and the random bits ensure that each ID is unique, even if thousands of people are creating push IDs at the same time.
然后回答你的第二个问题:
If I use the get(), is it always 100% guaranteed that the data I will get is sorted from oldest to the newest or will it be in lexicographical order?
如果加一个ValueEventListener or ChildEventListener or even if you call Query#get()也没关系,结果会按照同样的顺序排列。
但是,如果您需要根据时间组件明确排序,那么您应该添加一个 属性 类型的“timestamp”,正如我在以下 post 中的回答中所解释的:
如果您想颠倒顺序,请检查以下答案: