【问题标题】:Convert Firebase Dictionary Data to Array (Swift)将 Firebase 字典数据转换为数组 (Swift)
【发布时间】:2017-02-24 09:23:30
【问题描述】:

这可能是一个简单的答案,所以提前道歉,但我被困住了,因为我仍然对 Firebase 的工作方式感到困惑。我想根据保存在那里的 unix 日期数据查询 Firebase 数据库,然后获取相关的“唯一 ID”数据并将其放入数组中。

Firebase 中的数据如下所示:

posts
  node_0
    Unix Date: Int
    Unique ID Event Number: Int
  node_1
    Unix Date: Int
    Unique ID Event Number: Int
  node_2
    Unix Date: Int
    Unique ID Event Number: Int

到目前为止,我所拥有的如下。查询部分似乎按预期工作。我苦苦挣扎的地方是如何将“唯一 ID 事件编号”数据放入数组中。这是似乎最接近成功的方法,它基于此post,但我收到一个错误,即孩子没有“价值”成员。

// Log user in
if let user = FIRAuth.auth()?.currentUser {

    // values for vars sevenDaysAgo and oneDayAgo set here

    ...

    let uid = user.uid

    //Query Database to get the places searched by the user between one and seven days ago.
    let historyRef = self.ref.child("historyForFeedbackLoop/\(uid)")

    historyRef.queryOrdered(byChild: "Unix Date").queryStarting(atValue: sevenDaysAgo).queryEnding(atValue: oneDayAgo).observeSingleEvent(of: .value, with: { snapshot in

        if (snapshot.value is NSNull) {
            print("error")
        } else {
            for child in snapshot.children {
                if let uniqueID = child.value["Unique ID Event Number"] as? Int {
                    arrayOfUserSearchHistoryIDs.append(uniqueID)
                }
            }
        }
    })

} else {
    print("auth error")
}

非常感谢任何想法!

【问题讨论】:

    标签: ios arrays swift dictionary firebase


    【解决方案1】:

    我最终根据this post 中概述的方法重新设计了读取 Firebase 数据的方式。我使用的实际工作代码如下,以防对其他人有帮助。

    // Log user in
    if let user = FIRAuth.auth()?.currentUser {
    
       let uid = user.uid
       // values for vars sevenDaysAgo and oneDayAgo set here
    
       ...
    
       let historyRef = self.ref.child("historyForFeedbackLoop/\(uid)")
        historyRef.queryOrdered(byChild: "Unix Date").queryStarting(atValue: sevenDaysAgo).queryEnding(atValue: oneDayAgo).observeSingleEvent(of: .value, with: { snapshot in
    
            if (snapshot.value is NSNull) {
                print("user data not found")
            }
            else {
    
                 for child in snapshot.children {
    
                    let data = child as! FIRDataSnapshot
                    let value = data.value! as! [String:Any]
                    self.arrayOfUserSearchHistoryIDs.append(value["Unique ID Event Number"] as! Int)
                 }
            }
       })
    }
    

    【讨论】:

      【解决方案2】:

      尝试使用这个:-

         historyRef.queryOrdered(byChild: "Unix Date").queryStarting(atValue: sevenDaysAgo).queryEnding(atValue: oneDayAgo).observeSingleEvent(of: .value, with: { snapshot in
      
             if let snapDict = snapshot.value as? [String:AnyObject]{
      
                     for each in snapDict{
      
                             let unID = each.value["Unique ID Event Number"] as! Int   
                             arrayOfUserSearchHistoryIDs.append(unID)  
                          }
      
                    }else{
                     print("SnapDict is null")
                }
      
              })
      

      【讨论】:

      • 宾果,班戈,邦戈。这是 snapshot.value 不是孩子,呃。谢谢!
      • 其实我说得太早了。它不起作用,因为我收到“let unID =”行的“下标使用不明确”的错误。我尝试应用这篇文章中的想法,但没有成功。有任何想法吗? stackoverflow.com/questions/36238507/…
      • 你是用 swift 2 还是 swift 3 编码?对于 swift 2 使用 each.1[Unique ID Event Number] ,我也可以建议不要在您的数据库名称中使用空格,这会导致错误。将其更改为 Unique_ID_Event_Number
      • Swift 3. 关于空格的要点。我会从那个过渡。您认为这是导致此处错误的原因还是您只是对此发表评论?
      • 只是一般...你是在编译之后还是之前在 viewController 本身上收到错误。
      猜你喜欢
      • 2018-11-22
      • 1970-01-01
      • 2016-05-15
      • 2018-01-08
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多