【问题标题】:Getting value of children with same name - Firebase Swift3获取同名儿童的价值 - Firebase Swift3
【发布时间】:2017-03-10 04:38:33
【问题描述】:

我正在使用以下查询来获取下面的快照。

dbRef.child("users").queryOrdered(byChild: "username").queryStarting(atValue: text).queryEnding(atValue: text+"\u{f8ff}").observe(.value, with: { snapshot in })

但我无法弄清楚如何迭代孩子“用户名”(即比尔、比利、比利金斯)的值,因为它们都有相同的键(“用户名”)。我正在尝试提取所有名称并将它们放入一个名称数组中。

Snap (users) {
   fDrzmwRUt2guh3O8pc792ipyEqA3 =     {
       username = bill;
   };
   qyQkIOxSpXgQoUoh0QNvPlkQ1Mp1 =     {
       username = billy;
   };
   edSk54xSpXgQoYoh0QNvOlkQ1Mp3 =     {
       username = billykins;
   };
}

这是我的函数,它获取快照并尝试对其进行迭代。但是,循环崩溃了:

func findUsers(text: String) {
        print("search triggered")
        dbRef.child("users").queryOrdered(byChild: "username").queryStarting(atValue: text).queryEnding(atValue: text+"\u{f8ff}").observe(.value, with: { snapshot in

        print(snapshot)

        for i in snapshot.children{
            let value = snapshot.value as! Dictionary<String,String>
            let username = value["username"]
            print(username)
            self.userList.append(username!)
            print(self.userList)
        }
    }) { (error) in
        print("searchUsers \(error.localizedDescription)")
    }
}

这是日志的一小部分。太长无法发布:

0x1002cg360 <+272>: adr    x9, #268929               ; "value type is not bridged to Objective-C"
0x1002cg364 <+276>: nop    
0x1002cg368 <+280>: str    x9, [x8, #8]
->  0x1002cg36c <+284>: brk    #0x1
0x1002cg370 <+288>: .long  0xffffffe8                ; unknown opcode

【问题讨论】:

  • 对用户名使用 orderby 查询
  • 您尝试过什么了吗? Firebase 文档通常是一个不错的起点:firebase.google.com/docs/database/ios/…。如果您更喜欢动手实践的方法,请尝试适用于 iOS 的 Firebase 代码实验室:firebase.google.com/docs/samples/#codelabs
  • 这实际上是我获取快照的方式。我刚刚编辑了我的问题。我应该提到这一点。
  • @DanielGrigsby 您希望所有用户的名字都以bill 开头?
  • @NiravD 你是对的。我只想要以“bill”开头的名字。但是我的查询结果已经只有以 bill 开头的用户名了。这就是 .queryStarting(at: text) 和 .queryEnding(at: text) 的用途。我的问题是我只需要从快照中提取名称(bill、billy、billykins)。

标签: swift loops firebase swift3 firebase-realtime-database


【解决方案1】:

这是我正在寻找的解决方案。我需要遍历大快照以将子快照提取到数组中,然后按值循环遍历子快照以获取用户名:

func findUsers(text: String) {
    print("search triggered")
    dbRef.child("users").queryOrdered(byChild: "username").queryStarting(atValue: text).queryEnding(atValue: text+"\u{f8ff}").observe(.value, with: { snapshot in

        print(snapshot)

        if ( snapshot.value is NSNull ) {
            print("not found")
        } else {

            var blaArray = [FIRDataSnapshot]()

            for child in snapshot.children {
                let snap = child as! FIRDataSnapshot //downcast
                blaArray.append(snap)
            }

            for child in blaArray {
                let dict = child.value as! NSDictionary
                let username = dict["username"]!
                print(username)

            }
    }) { (error) in
        print("searchUsers \(error.localizedDescription)")
    }
}

改编自这篇文章:Get data out of array of Firebase snapshots

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多