ios - Firebase 查询 - 我得到某种类型的 NSArray 作为结果(只是有时)

标签 ios swift firebase firebase-realtime-database

所以我有一个这样的 firebase 数据库:

userIndexes
-- 1: uid1
-- 2: uid2
-- 3: uid3

我想通过 key 获取uid。所以我会这样查询:

DataService.sharedInstance.DB_REF_USERS_INDEXED_BY_ID.queryOrderedByKey().queryStarting(atValue: "2").queryLimited(toFirst: 1).observeSingleEvent(of: .value, with: { (snapshot) in
     print(snapshot.value)
})

当我使用 2 时或 3作为起点,我得到了这个打印品(这里是 2 ):

Optional({ 2 = uid2; })

我可以轻松地将其转换为 Dictionary<String, AnyObject>

但是当我使用 1作为起点,我明白了:

optional(<__NSArrayM 0x6000000494b0>( < null >, uid1 ) ) If I try to cast this to a Dictionary, it just gets nil

这是为什么?

最佳答案

我在 forum 中问过同样的问题..

You should avoid arrays (specifically, numeric, sequential ids) in distributed systems.

有关 Firebase 中如何处理类似数组的行为以及为什么应避免使用数组的更多信息:

它在旧的 firebase 文档中明确定义了在 firebase 中使用数组时发生的情况。检查Arrays in firebase Database

你也可以通过Best Practices: Arrays in Firebase .. 上面写着

if all of the keys are integers, and more than half of the keys between 0 and the maximum key in the object have non-empty values, then Firebase will render it as an array.

关于ios - Firebase 查询 - 我得到某种类型的 NSArray 作为结果(只是有时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39122499/

相关文章:

ios - 将 UIView 移动到新的 super View 并从 UITableViewCell 返回

swift - 手动添加框架后缺少所需模块

android - 具有多个模块的 Android 上的 Firebase 推送通知

swift - 从 firebase 下载图像链接时得到零

ios - 为什么 UIButton 不响应触摸?

iOS NSDictionary 值确定 bool 值是否来自 JSON bool 值

swift - 如何通过点表示法访问未实例化的对象? ( swift Playground )

javascript - Firebase OnAuthStateChanged 函数不返回任何内容

ios - Swift:我必须调用 Facebook 登录按钮两次才能获取 FBSDKAccessToken

iOS + Swift,如何访问所有音乐文件