简体   繁体   English

Firebase如何检查快照的子代本身是否为Datasnapshot类型

[英]Firebase how to check if snapshot's children are of type Datasnapshot themselves

How can I check if the child LojqctqkDpthbF_TYvP has children that are other Datasnapshot s and not k/v pairs like in eg. 如何检查子LojqctqkDpthbF_TYvP是否具有其他Datasnapshot s而不是k/v对的子项,例如在 1 ? 1个

I tried all of these but none worked: 我尝试了所有这些,但没有一个起作用:

Database.database().reference().child("LojqctqkDpthbF_TYvP").observeSingleEvent(of: .value, with: { (snapshot) in

    if !snapshot.hasChildren() { return }

    guard snapshot.children.allObjects is [DataSnapshot] else { return }

    guard let children = snapshot.children.allObjects as? [DataSnapshot] else { return }

    print("this print statement should only get reached if there are Datasnapshots underneath of it")

})

In the above code (from eg. 1 below) the print statement gets reached because there are children but they are only dict values. 在上面的代码中( 例如,从下面的1 ),由于有子级,所以达到了print语句,但它们只是dict值。 I want it so that the it doesn't get reached specifically because there aren't any Datasnapshots underneath of it. 我希望它不会被明确达到,因为它下面没有任何Datasnapshots。

eg. 例如。 1 Children that are dict values and no Datasnapshots 1个是dict值且没有数据快照的子级

-LojqctqkDpthbF_TYvP
  |
  |---"key1":"value1"
  |---"key2":"value2"
  |---"key3":"value3"

The print statement should only get reached if there are Datasnapshots like in eg. 仅在有诸如例如的数据快照时,才可以到达print语句 2 below 2以下

eg. 例如。 2 Children that are dict values and 2 Datasnapshots 2个是dict值的子级和2个数据快照

-LojqctqkDpthbF_TYvP
  |
  |---"key1":"value1"
  |---"key2":"value2"
  |---"key3":"value3"
  |
  @-LlzB3_ppHHVmdipXs
  |     
  @-LlzD8p5-WOT-ZZhfc

You're using definitions that are slightly off, which makes it hard to implement. 您使用的定义稍有偏差,这使其难以实现。

Let's first set the correct definition of what data in the database becomes a DataSnapshot in the client: 首先,我们为数据库中哪些数据成为客户端中的DataSnapshot设置正确的定义:

Say you have this JSON in the database: 假设您在数据库中有此JSON:

"-LojqctqkDpthbF_TYvP": {
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
}

If you load this JSON from the database into the app, you get a single top-level snapshot with key -LojqctqkDpthbF_TYvP . 如果将此JSON从数据库加载到应用程序中,则将获得一个带有键-LojqctqkDpthbF_TYvP的单个顶级快照。 This snapshot has three child snapshots, one for each key under it. 此快照有三个子快照,每个子快照一个。 The fact that the value of each of those keys is a primitive string value, doesn't matter: they're still each a DataSnapshot . 这些键中的每个键的值都是原始字符串值的事实并不重要:它们仍然是每个DataSnapshot So in the above JSON there are four snapshots, with keys -LojqctqkDpthbF_TYvP , key1 , key2 , and key3 . 因此,在上面的JSON中,有四个快照,它们的键为-LojqctqkDpthbF_TYvPkey1key2key3


The difference is in the type of the value of the child DataSnapshot s. 区别在于子DataSnapshot的值的类型。 So if you want to know if he value of a certain DataSnapshot is itself a complex type again, you can do something like for each child snapshot : 因此,如果您想知道某个DataSnapshot值本身是否又是复杂类型,则可以对每个子快照执行类似的操作

snapshot?.value is Dictionary

Alternatively you can check for each child snapshot whether they have children: 另外,您可以检查每个子快照是否有子:

snapshot?.hasChildren()

For -LojqctqkDpthbF_TYvP the above tests will both be true, while for the other keys they'll be false. 对于-LojqctqkDpthbF_TYvP ,上述测试都将为true,而对于其他键,它们将为false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM