Firebase仅加载一次数据并使用完成处理程序关闭
我决定使用Firebase作为我正在制作的应用程序的后端。 我意识到的一件事是,一旦我的表视图数据被填充,与Firebase的网络连接就不会关闭。
有没有办法可以关闭连接,这样就不会耗尽我的用户数据? 在我获取数据的代码中,我将其存储在本地,因此它仍然存在。
class HomeTableViewController: UITableViewController{ //firebase refrences var restaurantArray = [Restaurant]() var dataBaseRef: FIRDatabaseReference! { return FIRDatabase.database().reference() } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// override func viewDidLoad() { super.viewDidLoad() title = "Home" navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "menuIcon"), style: .plain, target: self, action: #selector(SSASideMenu.presentLeftMenuViewController)) navigationItem.leftBarButtonItem?.setBackButtonBackgroundImage(#imageLiteral(resourceName: "backButton"), for: .normal , barMetrics: .default) tableView.backgroundView = UIImageView(image: UIImage(named: "Background")) } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(true) fetchRestaurants() } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// func fetchRestaurants(){ dataBaseRef.child("AthensRestaurants/Restaurants").observe(.value, with: { (snapshot) in var results = [Restaurant]() for res in snapshot.children{ let res = Restaurant(snapshot: res as! FIRDataSnapshot) results.append(res) } self.restaurantArray = results.sorted(by: { (u1, u2) -> Bool in u1.name Int { // #warning Incomplete implementation, return the number of rows return restaurantArray.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "restaurantsCell", for: indexPath) as! RestaurantsTableViewCell // Configure the cell... cell.configureCell(res: restaurantArray[indexPath.row]) cell.backgroundColor = UIColor.clear cell.contentView.layer.borderColor = UIColor.clear.cgColor cell.contentView.layer.borderWidth = 1.5 return cell } //transfers data to new page func showRestaurantViewControllerWith(_ res: Restaurant) { let storyBoard = UIStoryboard(name: "Main", bundle: nil) let destinationVC = storyBoard.instantiateViewController(withIdentifier: "RestaurantDetails") as! RestaurantDetailViewController destinationVC.resTransfer = res self.navigationController?.pushViewController(destinationVC, animated: true) } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { self.showRestaurantViewControllerWith(self.restaurantArray[indexPath.row]) } }
你打电话时:
dataBaseRef.child("AthensRestaurants/Restaurants").observe(.value
您开始观察数据库中Restaurants
节点的值。 您的代码块将立即使用当前值运行,然后在值更改时运行 。 因此,Firebase数据库客户端将保持与服务器的开放连接。
如果您不需要更新值,可以向观察者注册:
dataBaseRef.child("AthensRestaurants/Restaurants"). observeSingleEvent(of: .value
请参阅Firebase文档中的一次读取数据 。
这将确保您只获得初始值,而不是等待更新。 但是关闭连接可能还需要很长时间。
要自己明确管理连接的打开/关闭,可以调用goOffline()
/ goOnline()
。 有关goOffline()
信息,请参阅Firebase参考文档 。
我认为您应该使用以下方法,因此您只加载一次数据:
dataBaseRef.child("AthensRestaurants/Restaurants").observeSingleEvent(of: .value, with: { (snapshot) in var results = [Restaurant]() for res in snapshot.children{ let res = Restaurant(snapshot: res as! FIRDataSnapshot) results.append(res) } self.restaurantArray = results.sorted(by: { (u1, u2) -> Bool in u1.name < u2.name }) self.tableView.reloadData() self.dataBaseRef.removeAllObservers() }) { (error) in print(error.localizedDescription) }