ios - Firebase 数据库查询以进行特定选择

标签 ios swift firebase firebase-realtime-database

我尝试在 xcode 上对 Firebase 数据库进行选择并将结果显示在表格中,但得到的结果非常奇怪。就像它在同一个条目上运行 for 循环一样。很可能是因为 table 。

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath)
let groceryItem = items[indexPath.row]

// Below is code to query users in Firebase to check if it is the current user.

let selection = ref.queryOrdered(byChild: "addedByUser").queryEqual(toValue : user.email)

selection.observeSingleEvent(of: .value, with:{ (snapshot: FIRDataSnapshot) in
  if let value = snapshot.value as? [String: Any] {
    print(value)
  }
})
//End of code


//if user.email == groceryItem.addedByUser {
cell.textLabel?.text = groceryItem.name

cell.detailTextLabel?.text = groceryItem.addedByUser
// }
toggleCellCheckbox(cell, isCompleted: groceryItem.completed)

return cell
}

Firebase 是:

 {
  "expense-items" : {
    "20170509" : {
      "addedByUser" : "michael_papado@crymary.gr",
      "amount" : 37,
      "categoryType" : "Shopping",
      "completed" : false,
      "name" : "Dijon",
      "paymentType" : "Cash"
    },
    "zambon" : {
      "addedByUser" : "harry@crymary.gr",
      "amount" : 13,
      "categoryType" : "Shopping",
      "completed" : false,
      "name" : "zambon",
      "paymentType" : "CreditCard"
    },
    "zouzouni" : {
      "addedByUser" : "harry@crymary.gr",
      "amount" : 24,
      "categoryType" : "Kids",
      "completed" : false,
      "name" : "zouzouni",
      "paymentType" : "Cash"
    }
  }
}

只有 20170509 来自登录的用户,我得到的条目是 20170509 条目的 3 倍。 如何计算选择的结果以便用正确的结果填充表格?

最佳答案

所以大家的想法都是错误的。我不应该检查 tableView 函数上的列表。 我所要做的就是在 viewDidLoad 中填充一个新列表并一直使用它。

    if FIRAuth.auth()?.currentUser != nil {
  let selection = ref.queryOrdered(byChild: "addedByUser").queryEqual(toValue : FIRAuth.auth()?.currentUser?.email)

      selection.observeSingleEvent(of: .value, with:{ (snapshot: FIRDataSnapshot) in

        var newItems: [ListItem] = []

        for item in snapshot.children {
          let listItem = ListItem(snapshot: item as! FIRDataSnapshot)
          newItems.append(listItem)
        }

        self.items = newItems

        self.tableView.reloadData()
      })
}

关于ios - Firebase 数据库查询以进行特定选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46181256/

相关文章:

android - RecyclerView 不从 Firebase Datasnapshot 检索数据

swift - 通过电子邮件发送密码重置代码 - Swift + Firebase

ios - 保持 iAds 横向 SpriteKit/Swift

ios - xcode 8 swift 3 latlong 在模拟器上工作但不在设备中工作

ios - 解析 PFQuery 使用 PFUSER.query 获取用户数据

swift - 不使用预定义函数反转字符串

ios - Swift 以编程方式将属性添加到实体

java - Firebase Firestore 在运行时错误时崩溃

ios - UITableView 中的单元格只有在 iOS 中选择后才会更新(快速)

ios - 如何在 Swift 中编辑 textField 时刷新键盘/returnKey?