2017-11-24 13 views
-2

私は素早くfirebaseサービスに新しいです 私はデータベースとしてfire storeデータベースを使用しています。すべてのデータを読み込んで素敵なテーブルビューに入れる最初のテーブルビューを持っています。私のテーブルビューのすべてのドキュメントにはサブコレクションがあります。ユーザーが行を押すと、サブコレクションを持つ2番目のテーブルビューを開きます。セグを使用してデータを転送

これは私のセグエコードの準備です:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

    if let indexPath = tableViewDishes.indexPathForSelectedRow { 

    db.collection("Restaurants").document("Applebees").collection("Menu").document(sections[indexPath.section].sectionName!).collection("Dishes").document(sections[indexPath.section].listofDishes![indexPath.row].DishName).collection("Options").getDocuments { (querySnapshot, error) in 
      if error != nil {print(error)} 
      else { 

       for document in querySnapshot!.documents { 

        //adding all the data to an array called myOption 

       } 
      } 
     } 

     let selectedDishTableViewController = segue.destination as! SelectedDishViewController 
     selectedDishTableViewController.myOption = self.myOption 
     selectedDishTableViewController.dish = self.sections[indexPath.section].listofDishes?[indexPath.row] 
     selectedDishTableViewController.sectionName = sections[indexPath.section].sectionName! 
     self.myOption.removeAll() 
    } 
} 

問題は私のコードたらdb.collectionラインに到達myOptionのみ、空の配列であるとするとき、それはループのために、すぐ後にジャンプしていることですそれは戻って、私の配列にオブジェクトを追加します。

最初に行を押すと、2番目のテーブルビューが空になり、戻ってもう一度押すと必要な情報が表示されます。

+0

すべての 'db.collection(...)'行が非同期呼び出しを行っています。これは実際にクロージャーに入ります(ここでは 'querySnapshot!.documents'の' for document 'を書きました)。クエリを実行した後にのみ 'performSegue()'を呼び出します。 – Larme

答えて

0

db.collection()いくつかの非同期作業を行うため、すべてのコード:

let selectedDishTableViewController = segue.destination as! SelectedDishViewController 
selectedDishTableViewController.myOption = self.myOption 
selectedDishTableViewController.dish = self.sections[indexPath.section].listofDishes?[indexPath.row] 
selectedDishTableViewController.sectionName = sections[indexPath.section].sectionName! 
self.myOption.removeAll() 

はあなたがmyOption配列を設定するためのループの前に右でなければなりません。そうすれば、データベースがすべてのデータを取得し、すべての設定をforループの中に入れたら、すべてが設定されます。その非同期以来

0
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

    if let indexPath = tableViewDishes.indexPathForSelectedRow { 

    db.collection("Restaurants").document("Applebees").collection("Menu").document(sections[indexPath.section].sectionName!).collection("Dishes").document(sections[indexPath.section].listofDishes![indexPath.row].DishName).collection("Options").getDocuments { (querySnapshot, error) in 
      if error != nil {print(error)} 
      else { 

       for document in querySnapshot!.documents { 

        //adding all the data to an array called myOption 

       } 

     let selectedDishTableViewController = segue.destination as! SelectedDishViewController 
     selectedDishTableViewController.myOption = self.myOption 
     selectedDishTableViewController.dish = self.sections[indexPath.section].listofDishes?[indexPath.row] 
     selectedDishTableViewController.sectionName = sections[indexPath.section].sectionName! 
     self.myOption.removeAll() 

      } 
     } 
    } 
} 

あなたは、コールバックでselectedDishTableViewControllerセグエを記述する必要があります呼び出します。

勧告:Sequeデリゲートメソッドはシンプルであるべきで、それを最適化するために、これらすべてのDB操作を試して取り扱うべきではありません。

+0

async db.collection()... getdocuments()の前に2番目のtableviewのviewdidloadが呼び出されているので、これは動作しません これを修正する方法はありますか? –

関連する問題