在Firebase数据库中搜索子子级 [英] Search for sub child in firebase database

查看:59
本文介绍了在Firebase数据库中搜索子子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firebase结构

Firebase Structure

您好,我正在尝试找出如何查询jobBrand&我的Firebase数据库(附加结构)中的jobName并将其存储在tableView中.我将在每个工作品牌下存储更多信息,所以如果可能的话,我想保持这种结构吗?

Hi, I am try to work out how to query jobBrand & jobName in my Firebase Database (Structure Attached) and store it in a tableView. I am going to store more information under each Job Brand so I would like to keep the structure this way if possible?

到目前为止,如果它们是上一级的,我可以让tableView读取2个字段,因此结构为:

So far, I can get tableView to read the 2 fields if they are one level up, so the structure would be:

tbaShootApp->职位->职位品牌>数据.

tbaShootApp -> Jobs -> Job Brand > data.

我无法计算出下一层并将其存储在tableView中.这可能吗?

I cannot work out to query down another level and store it in the tableView. Is this possible?

我正在使用字典来存储工作信息:

I am using a dictionary to store the job information:

class Job: NSObject {
var id: String?
var jobBrand: String?
var jobName : String?
var director : String?
var jobInfo : jobInfo?
init(dictionary: [String: AnyObject]) {
    self.id = dictionary["id"] as? String
    self.jobBrand = dictionary["jobBrand"] as? String
    self.jobName = dictionary["jobName"] as? String
    self.director = dictionary["Director"] as? String
}

}

这是查询数据的代码-我的superViewDidLoad中有函数'fetchJobs'.

Here is the code to query the data - I have the function 'fetchJobs' in my superViewDidLoad.

func fetchJobs() {

    Database.database().reference().child("Jobs").observe(.childAdded) { (snapshot) in

        if let dictionary = snapshot.value as? [String: AnyObject] {

                let job = Job(dictionary: dictionary)
                job.id = snapshot.key
                self.jobs.append(job)

                //this will crash because of background thread, use dispatch_async to fix
                DispatchQueue.main.async(execute: {
                    self.tableView.reloadData()
                })
            }
        }
    }

JSON

{
  "Jobs" : {
    "Candycrush" : {
      "cameraInfo" : {
        "cameraBody" : "A",
        "cameraBrand" : "Arri",
        "cameraType" : "Alexa"
      },
      "jobInfo" : {
        "jobBrand" : "CandyCrush",
        "jobName" : "Winter"
      }
    },
    "Honda" : {
      "cameraInfo" : {
        "cameraBody" : "A",
        "cameraBrand" : "Arri",
        "cameraType" : "Alexa XT"
      },
      "jobBrand" : "Honda",
      "jobName" : "Comet"
    },
    "WWF" : {
      "cameraInfo" : {
        "cameraBody" : "A",
        "cameraBrand" : "Red",
        "cameraType" : "Epic"
      },
      "jobInfo" : {
        "jobBrand" : "WWF",
        "jobName" : "Eye"
      }
    }
  }
}

推荐答案

更新

自从我发布答案以来,您已经添加了更多信息.现在很明显,您想进行更深入的查询.另外,由于您发布了实际的JSON,因此JobBrand节点现在很明显是动态的.您可以查询一个未知孩子下面的一个级别.您可以在此处中了解该信息.

Since you have added a lot more information since I posted my answer. It is now obvious that you want to query deeper. Additionally, since you posted the actual JSON it is now obvious that the JobBrand node is dynamic. You can query one level below an unknown child. You can read about that here.

我将把.observe(.childAdded)更改为.observeSingleEvent(of: .value),因为您查询的内容太深了,我怀疑您想观察该节点,但是我可能错了.更改此设置将仅一次提取数据.如果要更新该值,则必须再次查询.

I'm going to change .observe(.childAdded) to .observeSingleEvent(of: .value) because you are querying so deep I doubt you want to observe that node, but I could be wrong. Changing this will pull the data in once and only once. If you want to update that value you will have to query again.

您想要这样的查询:

Database.database().reference().child("Jobs").queryOrdered(b‌​yChild: "jobInfo/jobBrand").queryEqual(toValue: "JobBrandYouWant").observeSingleEvent(of: .value, with: { snapshot in
    if let dictionary = snapshot.value as? [String: AnyObject] {
        let job = Job(dictionary: dictionary)
        job.id = snapshot.key
        self.jobs.append(job)
    }
})

原始答案

您当前的查询将返回作业下的所有数据.其中包括节点Job Brand-> jobinfo->等.

You current query is going to return all of the data under jobs. Which includes the nodes Job Brand -> jobinfo -> etc.

如果您尝试获取较少的数据,则需要了解JobBrand,jobinfo或两者.但是,我认为您需要所有工作,并且查询无法正常工作.

If you are trying to get less data then you need to know either the JobBrand, jobinfo or both. However, I think you want all of the jobs and your query just isn't working.

您当前的查询失败,因为您的词典包含工作"下的所有内容,而不仅仅是一项工作.您想遍历数据快照,然后 在调用init之前先获得每项工作.

You current query fails because your dictionary contains everything under "Jobs" not just one job. You want to loop over the data snapshot and get each job before you call init.

Database.database().reference().child("Jobs").observe(.childAdded, with: { snapshot in 
    for child in snapshot.children { // over over the snapshot and cast each "job" to it's own dictionary
        let child = child as? DataSnapshot
        if let key = child?.key {
            if let dictionary = child?.value as? [String: AnyObject] {
                let job = Job(dictionary: dictionary)
                job.id = key
                self.jobs.append(job)
            }
        }
    }
})

文档提供了用于循环访问快照

The documentation provides this example for looping over a snapshot

_commentsRef.observe(.value) { snapshot in
  for child in snapshot.children {
    ...
  }
}

这篇关于在Firebase数据库中搜索子子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆