Firebase Structure
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?
So far, I can get tableView to read the 2 fields if they are one level up, so the structure would be:
tbaShootApp -> Jobs -> Job Brand > data.
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
}
}
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"
}
}
}
}