如何从 Firebase Async 返回 numberOfRowsInSection



我从Firebase.kids检索计数,数据是异步的,我能够从函数回调中检索计数,但我无法从回调中获取结果并返回行。我怎样才能完成此操作?提前谢谢你。

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {   
    var number:Int!
    getFirData { (result) in
        number = Int(result.childrenCount)
    }
    return number   
}

这是从Firebase检索的异步数据

func getFirData(completion:(result:FIRDataSnapshot)->()){
    let dbRef = FIRDatabase.database().reference()
    let userAiD = FIRAuth.auth()?.currentUser?.uid
    dbRef.child("posts").queryOrderedByChild("userAiD").queryEqualToValue(userAiD).observeEventType(.Value, withBlock: {snapshot in
        let snapshot = snapshot
        completion(result:snapshot )
    })
}

你不能。 您必须在viewDidLoad()viewWillAppear(_:)中触发该查询,将查询的值存储在属性中以供引用,并在表视图上调用reloadData()(或相关的插入方法)。

最新更新