如何从Firebase数据库获取数据

时间:2019-11-26 23:07:50

标签: swift firebase firebase-realtime-database

因此我试图连接数据库。因此,您将能够在搜索栏上搜索数据库中存在的人员。 但是它仍然没有连接

import UIKit
import Firebase
import FirebaseDatabase

class SearchViewController: UIViewController {
    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var tableView: UITableView!
    var postData = [String]()
    var ref: DatabaseReference!
    var databaseHandle: DatabaseHandle?

    override func viewDidLoad() {
        super.viewDidLoad()
     // let db = Firestore.firestore()
     // Do any additional setup after loading the view.

        ref = Database.database().reference()
        databaseHandle = ref?.child("users").observe(DataEventType .childAdded, with: { (snapshot) in
            // code to execute when child added under users
            let post = snapshot.value as? String
            if let actualPost = post {
                self.postData.append(actualPost)
                self.tableView.reloadData()
            }
        }
    }
}

0 个答案:

没有答案