Firebase查询 - 我得到一些类型的NSArray作为结果(有时只) [英] Firebase querying - I get some type of NSArray as result (only sometimes)

查看:112
本文介绍了Firebase查询 - 我得到一些类型的NSArray作为结果(有时只)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想要通过使用密钥。所以我会这样查询:
$ b $ pre $ $ $ $ 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 ):


可选({
2 = uid2;})

我可以很容易的将它转换为 Dictionary< String ,AnyObject>



但是当我使用 1
$ b


可选(< __ NSArrayM 0x6000000494b0>(< null>,
uid1))
如果我尝试把它转换成字典,它只是获得 nil


解决方案

我在论坛


您应该避免在分布式系统中使用数组(特别是数字,顺序标识符)。

有关如何在Firebase中处理类似数组的行为,以及为什么您应该避免使用数组:

在旧的firebase文档中明确定义,当您在Firebase中使用阵列时发生了什么。检查数据库中的数组

您也可以通过最佳实践:Firebase中的数组如果所有密钥都是


整数,并且在对象中的0和最大键之间的键的一半以上都有非空值,那么Firebase将把它作为一个数组来呈现。



so I've got a firebase database like this:

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

I want to get the uid by using the key. So I'll query it like this:

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

When I use 2 or 3 as starting point, I get this print (here it's 2):

Optional({ 2 = uid2; })

I easily can cast this to Dictionary<String, AnyObject>

But when I use 1 as starting point, I get this:

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

Why is that?

解决方案

I asked same question in forum ..

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

For more on how array-like behaviors are handled in Firebase and Why you should avoid arrays:

It clearly defines in old firebase doc., whats going on when you use Arrays in firebase. check Arrays in firebase Database

You can also go through Best Practices: Arrays in Firebase .. where it says

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.

这篇关于Firebase查询 - 我得到一些类型的NSArray作为结果(有时只)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆