Firebase with Swift 3 counting the number of children

前端 未结 3 842
遥遥无期
遥遥无期 2020-12-10 09:02

I have an array of strings and I am trying to populate it through firebase. It is a chat application and when a user creates a room he or she names the room. When the user l

3条回答
  •  醉话见心
    2020-12-10 09:41

    My way is to have the closure. When the call has completed then it will return the number of children

    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)
            }
        })
    
    }
    

提交回复
热议问题