iOS Firebase将异步数据检索到Array中

时间:2018-05-16 18:35:44

标签: ios swift firebase asynchronous

我尝试将Firebase中的数据检索到数组中。因为它是异步运行的,所以我想在CollectionView中显示的结果是一个延迟,直到我来回切换。我对iOS中的异步函数很新。请帮我完成我的代码。

    ref = Database.database().reference(withPath: "MyTest/Video")
        ref?.observeSingleEvent(of: .value, with: { snapshot in

            if !snapshot.exists() { return }

            if let result = snapshot.children.allObjects as? [DataSnapshot] {
                for child in result {
                    let autoID = child.key as String //get autoID

                    let title = snapshot.childSnapshot(forPath: "\(autoID)/Title").value
                    let url = snapshot.childSnapshot(forPath: "\(autoID)/URL").value
                    let views = snapshot.childSnapshot(forPath: "\(autoID)/Views").value

                    self.arrayAllTitle.append(title as! String)
                    self.arrayAllId.append(url as! String)
                    self.arrayAllDesc.append(views as! String)
                }
            }


        })

2 个答案:

答案 0 :(得分:1)

您需要在检索数据后重新加载集合,以便在for循环调用之后reloadData()

for child in result {

}

self.collectionView.reloadData()

//

func getValueFromDatabase(completion: @escaping (_ status: Bool) -> Void){

    ref = Database.database().reference(withPath: "MyTest/Video")
    ref?.observeSingleEvent(of: .value, with: { snapshot in

        if !snapshot.exists() { return }

        if let result = snapshot.children.allObjects as? [DataSnapshot] {
            for child in result {
                let autoID = child.key as String //get autoID

                let title = snapshot.childSnapshot(forPath: "\(autoID)/Title").value
                let url = snapshot.childSnapshot(forPath: "\(autoID)/URL").value
                let views = snapshot.childSnapshot(forPath: "\(autoID)/Views").value

                self.arrayAllTitle.append(title as! String)
                self.arrayAllId.append(url as! String)
                self.arrayAllDesc.append(views as! String)
            }

            completion(true)
        }
        else {
            completion(false)
        }
    })
}

//

self.getValueFromDatabase { (status) in

    if status {
       // success 
    }
 }

答案 1 :(得分:0)

我现在正在我的项目中使用Firebase。我建议使用以下解决方案:将数据库观察器包装在一个不同的函数中,该函数将完成块作为参数。

func getValueFromDatabase(completion: ()->Void){

    ref = Database.database().reference(withPath: "MyTest/Video")
    ref?.observeSingleEvent(of: .value, with: { snapshot in

        if !snapshot.exists() { return }

        if let result = snapshot.children.allObjects as? [DataSnapshot] {
            for child in result {
                let autoID = child.key as String //get autoID

                let title = snapshot.childSnapshot(forPath: "\(autoID)/Title").value
                let url = snapshot.childSnapshot(forPath: "\(autoID)/URL").value
                let views = snapshot.childSnapshot(forPath: "\(autoID)/Views").value

                self.arrayAllTitle.append(title as! String)
                self.arrayAllId.append(url as! String)
                self.arrayAllDesc.append(views as! String)
            }

            completion()
        }
    })
}

这样,您可以在从db完成数据提取后提供所需操作的任何地方调用该函数:

getValueFromDatabase(completion:{

    self.collectionView.reloadData() //or any other action you want to fulfil

})