2017-01-10 7 views
0

Firebaseからデータを取得するのに問題があります。私は3つのテストデータセットをセットアップしました、各アイテムは "キャンペーン"と呼ばれています。すべての「キャンペーン」には、キャンペーンid-> users-> userid-> confirmImageUrlの下に続くキャンペーンを完了したユーザーと、完了したイメージがあります。ユーザーの子と子を引き出そうとすると、すべてのデータが取得されません。ユーザーがいない場合は、それは正しいと考えています。 Firebaseで子供が多い子供を頼んだとき、Firebaseが間違ったスナップショットを返す?

これは、それはセットアップがどのようにある:私のviewDidLoadFirebase Data Setup

、これは私がデータを引っ張っています方法です。それぞれの「キャンペーン」UITableViewCellに入る:私はそれを理解しようとしている非常に長い時間を費やしている The Output from the snapshot received

DataService.ds.DATABASE_BASE_CAMPAIGNS.observe(.value, with: { (snapshot) in 
    print("menan check:\(snapshot.value!)") 
    if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] { 
     print("menan check: \(snapshot)") 
     self.campaigns = [] 
     for snap in snapshot { 
      if let campaignData = snap.value as? Dictionary <String, AnyObject> { 
       let key = snap.key 
       let campaign = Campaigns(campaignKey: key, campaignData: campaignData) 
       print("menan check: \(campaignData)") 
       self.campaigns.append(campaign) 
      } 
     } 
    } 
    self.tableView.reloadData() 
}) 

これが出力されますか?誰でも何が間違っているか知っていますか

答えて

0

データがかなり深いので、キー値のペアを扱うにはさらにいくつかの辞書が必要になります。

ここでは、確認値である最も深いデータに簡単にアクセスできます。それは短縮することができましたが、私はそれを冗長にして、行ごとに説明することができました。

campaignRef.observe(.value, with: { (snapshot) in 
      for snap in snapshot.children { //iterate over 21hb, 989 huv etc 

       //contains all data in 21hb 
       let node = snap as! FIRDataSnapshot 

       // represent the data in the node as a dictionary 
       let nodeDict = node.value as! [String: AnyObject] 

       //grab just the users node, whose value is also a dictionary 
       let users = nodeDict["users"] as! [String: AnyObject] 

       //iterate over each user in the users node 
       for user in users { 
        let userId = user.key //this is the user id 

        //the value is a dictionary of key: value 
        // as in confirm: url 
        let value = user.value as! [String: AnyObject] 

        //get the value for the confirm key, which is the url 
        let confirm = value["confirm"] 

        //print the user it and the confirm url 
        print("key = \(userId) value = \(confirm!)") 
       } 
      } 
    }) 
関連する問題