2016-01-31 31 views
0

我有一个“扁平化”的Firebase结构,并试图从“辅助”数据库成员中检索值的字典。换句话说,我有一个谈话,其中有一个“到”单元格,这个单元格是商业列表的关键。有了这个键,我试图检索商家信息及其子项(网址,描述,标题)。出于某种原因,我可以打印snapshot2.value,并以期望的值作出响应,但我无法将其传递给我的类初始化。在iOS问题中展开Firebase查询

DataService.ds.REF_CONVOS.observeEventType(.Value, withBlock: {snapshot in 
     self.convoListings.removeAll() 
     self.convoListings = [] 
     //Data parsing from Firebase. The goal is to breakdown the data received and store in a local model. 
     if let snapshots = snapshot.children.allObjects as? [FDataSnapshot] { 
      for snap in snapshots { 
       for convo in userConvos { 
       // Going into the children of the main object for the conversations. 
        //print("\(snap)") 
        if convo == snap.key { 
         //print(snap.value) 
         print(snap.value) 
         if let businessDict = snap.value as? Dictionary<String, AnyObject> { 
          businessName.removeAll() 
          let test = businessDict["to"] as? String 
          DataService.ds.REF_BusinessListing.childByAppendingPath(test).childByAppendingPath("title/").observeSingleEventOfType(.Value, withBlock: { snapshot2 in 
           print(snapshot2.value) 
          }) 
          let key = snap.key 
          let post = ConvoListing(convoKey: key, dictionary: businessDict, businessName: self.test2) 
          self.convoListings.append(post) 
         } 
        } 
       } 
      } 
     } 

     self.tableView.reloadData() 
    }) 

回答

0

你似乎嵌套的:

DataService.ds.REF_BusinessListing.childByAppendingPath(test).childByAppendingPath("title/").observeSingleEventOfType(.Value, withBlock: { snapshot2 in 
    print(snapshot2.value) 
}) 
let key = snap.key 
let post = ConvoListing(convoKey: key, dictionary: businessDict, businessName: self.test2) 
self.convoListings.append(post) 

牢记observeSingleEventOfType负载数据不同步。因此,如果您的代码需要您加载的值,则需要将该代码放入代码块中:

DataService.ds.REF_BusinessListing.childByAppendingPath(test).childByAppendingPath("title/").observeSingleEventOfType(.Value, withBlock: { snapshot2 in 
    print(snapshot2.value) 
    let key = snap.key 
    let post = ConvoListing(convoKey: key, dictionary: businessDict, businessName: self.test2) 
    self.convoListings.append(post) 
})