2017-12-18 25 views
1

我使用github做了一個測試應用程序,後面跟着Google的例子,但有一些變化(在firestore中使用的鍵較少,過濾器少一些)。在firestore中添加新密鑰時,應用程序崩潰

問題是這樣的,當我在firestore中添加新密鑰時,應用程序崩潰,但該應用程序使用了先前添加的兩個密鑰。

崩潰並顯示錯誤fatalError("error")。我不明白爲什麼有兩個鍵的應用程序工作,但如果我開始使用第三個鍵(大廳),那麼應用程序崩潰。

可能是什麼問題?

這是我的代碼:

class ViewControllerTwo: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    @IBOutlet var tableView: UITableView! 

    private var sweets: [Sweet] = [] 
    private var document: [DocumentSnapshot] = [] 

    fileprivate var query: Query? { 
     didSet { 
      if let listener = listener { 
       listener.remove() 
      } 
     } 
    } 

    private var listener: FIRListenerRegistration? 

    fileprivate func observeQuery() { 

     guard let query = query else { return } 

     stopObserving() 

     listener = query.addSnapshotListener { [unowned self] (snapshot, error) in 

      guard let snapshot = snapshot else { 

       print("Error fetching snapshot results: \(error!)") 

       return 

      } 

      let models = snapshot.documents.map { (document) -> Sweet in 

       if let model = Sweet(dictionary: document.data()) { 

        return model 

       } else { 

        fatalError("error") 

       } 

      } 

      self.sweets = models 

      self.document = snapshot.documents 

      self.tableView.reloadData() 

     } 

    } 

    @IBAction func filterButton(_ sender: Any) { 
     present(filters.navigationController, animated: true, completion: nil)  
    } 

    lazy private var filters: (navigationController: UINavigationController, filtersController: FilterViewController) = { 
     return FilterViewController.fromStoryboard(delegate: self) 
    }() 

    fileprivate func stopObserving() { 
     listener?.remove() 
    } 

    fileprivate func baseQuery() -> Query { 
     return Firestore.firestore().collection("sweets").limit(to: 50)  
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     query = baseQuery()  
    } 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(true) 

     observeQuery() 
    } 

    override func viewWillDisappear(_ animated: Bool) { 
     super.viewWillDisappear(true) 

     stopObserving()  
    } 

    deinit { 
     listener?.remove()  
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     return sweets.count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ViewControllerCell 

     let sweet = sweets[indexPath.row] 

     cell.studioNameLabel.text = sweet.name 
     cell.studioAddressLabel.text = sweet.content 
     cell.hallNameLabel.text = sweet.hall 

     return cell 

    } 

} 

extension ViewControllerTwo: FiltersViewControllerDelegate { 

    func query(withCategory title: String?) -> Query { 

     var filtered = baseQuery() 

     if let title = title, !title.isEmpty { 
      filtered = filtered.whereField("title", isEqualTo: title) 
     } 

     return filtered 
    } 

    func controller(_ controller: FilterViewController, didSelectCategory title: String?) { 

     let filtered = query(withCategory: title) 

     self.query = filtered 
     observeQuery() 

    } 
} 

class ViewControllerCell: UITableViewCell { 

    @IBOutlet weak var studioNameLabel: UILabel! 
    @IBOutlet weak var studioAddressLabel: UILabel! 
    @IBOutlet weak var hallNameLabel: UILabel! 

} 

而且我的結構:

protocol DocumentSerializable { 
    init?(dictionary:[String:Any]) 
} 
struct Sweet { 
    var name:String 
    var content:String 
    var hall:String 
    var dictionary:[String:Any] { 
     return [ 
      "name": name, 
      "content" : content, 
      "hall" : hall 
     ] 
    } 
} 
extension Sweet : DocumentSerializable { 

    static let title = [ 
     "one", 
     "two", 
     "three", 
     "four" 
    ] 
    init?(dictionary: [String : Any]) { 
     guard let name = dictionary["name"] as? String, 
      let content = dictionary["content"] as? String, 
      let hall = dictionary["hall"] as? String else { return nil } 

     self.init(name: name, content: content, hall: hall) 
    } 
} 

我在谷歌項目帶動

google drive

google service info.plist

+0

嘗試打印'Sweet(dictionary:document.data())'並檢查它是否爲'nil' –

+0

@DharmeshKheni in'fatalError()'? – Dima

+0

以上'如果讓模型=甜(字典:document.data())' –

回答

0

只需向現有結構中添加任何新密鑰,您就需要重新安裝應用程序。

所以你應該在結構實現之前決定你需要什麼鍵。或者,如果將來添加新密鑰,則可以重新安裝應用程序。

相關問題