iOS 问题中扁平化的 Firebase 查询



我有一个"扁平化"的Firebase结构,并试图从"辅助"数据库成员中检索值字典。换句话说,我有一个对话,其中包含一个"to"单元格,该单元格具有企业列表的密钥。使用此键,我正在尝试检索商家信息及其子项(网址、描述、标题)。出于某种原因,我可以打印 snapshot2.value 并且它以预期值响应,但我无法将其传递给我的类初始化。

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()
    })

您的嵌套似乎:

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)
})

最新更新