【问题标题】:Firebase with Swift 3 counting the number of childrenFirebase 与 Swift 3 计算孩子的数量
【发布时间】:2016-10-21 10:58:33
【问题描述】:

我有一个字符串数组,我正在尝试通过 firebase 填充它。它是一个聊天应用程序,当用户创建房间时,他或她会为房间命名。当用户登录并进入登录页面时,它会查询他或她参与的所有房间,我希望它能够填充 tableview。在 firebase 文档中,我找到了 childrenCount,但我似乎无法让它工作。这是我迄今为止尝试过的

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    let ref = firebase.child("users").child(fUID).child("participating")

    ref.observe(.value, with: { (snapshot: FIRDataSnapshot!) in
        print(snapshot.childrenCount)
        rooms.count = snapshot.childrenCount
    })

    return rooms.count
}

我得到一个错误,计数是一个只能获取的属性。 如何填充该数组计数?

【问题讨论】:

  • 你不能这样设计tableview,在调用tableView.reloadData()之前必须初始化变量“room”,函数numberOfRowsInSection应该只包含一行代码“return rooms.count”和您以不同的功能修改房间(例如在 viewDidLoad 中)
  • 因为看起来你的代码的问题是这个rooms.count = snapshot.childrenCount
  • @MazelTov 是的,问题出在那条线上,知道如何解决吗?
  • 有几种方法可以做到这一点,我会在 viewDidLoad 中执行 Firebase 查询并将房间保存在某个数组中......在 numberOfRowsInSection 中,您将只返回对象...

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

我猜“房间”是一个房间类的数组。

当您尝试设置数组“房间”的范围时,会发生“count is a get only property 的错误”——您不能这样做。

'count' 属性是只读访问。 在人的语言中,它就像。你有一个包。你在袋子里放了一个苹果。你在袋子里放了另一个苹果。现在你有 2 个苹果。你不能只说“我的包里有 2 个苹果”。

修复它:

  • 您必须为每个快照创建类型为“Room”类的变量:FIRDataSnapshot
  • 将其添加到房间。

例子:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

let ref = firebase.child("users").child(fUID).child("participating")

ref.observe(.value, with: { (snapshot: FIRDataSnapshot!) in
    print(snapshot.childrenCount)

    let room = Room()
    // Use you snapshot(FIRDataSnapshot) to create the data of the room.
    rooms.append(room)
})

return rooms.count

}

【讨论】:

  • 我在 let room = Room()..use of unresolved identifier 上收到错误
  • 这是不正确的,因为您在 numberOfRowsInSection 中调用异步调用,该调用需要同步返回而无需等待。更好的方法是在 viewDidLoad 或类似的设置时间进行 Firebase 调用,将数据存储在属性中并在回调块调用 tableView.reloadData
【解决方案2】:

Firebase 数据以异步方式加载(和同步)。如果您添加一些调试日志记录,这是最容易看到的:

let ref = firebase.child("users").child(fUID).child("participating")

print("Starting observing");
ref.observe(.value, with: { (snapshot: FIRDataSnapshot!) in
    print("Got snapshot");
    print(snapshot.childrenCount)
    rooms.count = snapshot.childrenCount
})

print("Returning count");
return rooms.count

当你运行这个 sn-p 时,日志输出将是:

开始观察

返回次数

得到快照

这可能不是您期望的输出顺序。它还解释了为什么您的计数永远不会正确:数据尚未加载,因此无法计数。

这就是 Firebase 侦听器使用回调块的原因:数据同步时调用块。

【讨论】:

  • 谢谢,我没有尝试这样做,而是将所有孩子拉到一个数组中并得到了它的计数
【解决方案3】:

我的方法是关闭。调用完成后,它将返回孩子的数量

typealias countAllPostsResult = (UInt) -> Void
class func countAllPosts(isDeleted: Bool,completedHandler: @escaping countAllPostsResult) {
    print("countAllPosts ...is running...", isDeleted)
    var dataSnapshot = [DataSnapshot]()

    Constants.Commons.posts_node_ref.queryOrdered(byChild: Constants.Posts.is_deleted).queryStarting(atValue: "false").queryEnding(atValue: "false\u{f8ff}").observeSingleEvent(of: .value, with: { (snapshot) -> Void in

        for snap in snapshot.children {
            dataSnapshot.append(snap as! DataSnapshot)
        }
        if dataSnapshot.count > 0 {
            completedHandler(UInt(dataSnapshot.count))
        } else {
            completedHandler(0)
        }
    })

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    相关资源
    最近更新 更多