【问题标题】:Getting all the users same child in Firebase (Swift)在 Firebase (Swift) 中获取所有用户相同的孩子
【发布时间】:2016-07-20 10:13:20
【问题描述】:

我目前正在 Firebase 中构建一个应用,我想获得所有用户的菠萝率。

这是树:

user1: uid1 {
  fruits: {
    apple: 3
    pineapple: 5
  }
}

user2: uid2 {
  fruits: {
    apple: 4
    pineapple: 2
  }
}

我试过这段代码:

var ref = FIRDatabase.database().reference()
ref.queryOrderedByChild("fruits")
            .observeSingleEventOfType(.Value, withBlock: { (snapshot) in
                if ( snapshot.value is NSNull ) {
                    print("not found")
                } else {
                    for child in snapshot.children {
                        let rate = child.value["pineapple"] as! String
                        print(rate)
                    }
                }
            })

但它给了我一个打开 Nil 的错误。

结构图:

【问题讨论】:

  • 能否请您发布您的结构
  • @EICaptainv2.0 在帖子中添加
  • 好的,让我检查一下

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

应该是这样的

  var ref = FIRDatabase.database().reference()
     ref.queryOrderedByChild("user2")
        .observeSingleEventOfType(.Value, withBlock: { (snapshot) in
            if snapshot.exists() {
                for child in (snapshot.value?.allValues)! {
                    if let fruits = child["Fruits"] as? [String:String]{
                        let rate = fruits["pineapple"]
                        print(rate)
                    }
                }
            }
        })

【讨论】:

  • 为什么fruitsuserid在同一个主干?我想要user树干里的水果
  • fruits 是每个孩子都可以使用的对象 .. 与用户 ID 相同...所以您的结构应该是这样的...而不是在用户内部制作水果...它的结构非常复杂且不必要太
  • 如果你不想制作这个结构......完全独一无二......它应该在 key:value 对中
  • 如果你能做到就好了,因为我不知道树干果实和用户是什么
  • 那我该如何实现user2和他的水果率?
【解决方案2】:

看来这只需要深度查询?

    let ref = self.myRootRef.child("parent_node")

    ref.queryOrderedByChild("Fruits/pineapple").observeSingleEventOfType(.Value, withBlock: { 
       snapshot in

        if ( snapshot.value is NSNull ) {
            print("not found")
        } else {
            for child in snapshot.children {

                let key = child.key as String
                let dict = child.value["Fruits"] as! NSDictionary
                let pineapple_count = dict["pineapple"] as! Int
                print("key = \(key)  count = \(pineapple_count)")

            }
        }
    })

输出是

key = uid_1  count = 1
key = uid_0  count = 3
key = uid_2  count = 21

【讨论】:

  • 这合法吗?:Fruits/pineapple
  • @Eliko 是!这是形成深度查询的一种方法。试试我的代码,它已经过测试了。
【解决方案3】:

在下面试用这段代码(Xcode 8.3.3、Swift 3 和 Firebase 3)

   var ref = Database.database().reference().child("fruits")

   ref.observeSingleEvent(of: .value, with: {                
                (snapshot) in
         if let fruitPost = snapshot.value as? Dictionary<String,AnyObject> {
                 for(key, value) in fruitPost {
                      if let fruitData = value as? Dictionary<String,AnyObject> {
                             if let fruits = fruitData["pineapple"] as? String{
                                     let rate = fruits
                                        print(rate)
                                   }            
                              }
                          }
                    }

祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多