I am new to Swift 3 and Firebase so am having some issues with retrieving data from two different nodes and displaying them in one table view cell. I have found some other similar questions on Stack Overflow like here:
Firebase - iOS Swift: load table view cell with data retrieved from two separate child nodes
but could not apply it to my code as either these examples were too specific to the person asking or my lack of knowledge around Firebase has prevented me from progressing with the answers supplied.
My application is written in Swift 3 using Xcode-8 and uses Firebase for data persistence. The aim of the application is to allow users to submit different exercise programs for consumption by other users. User submitted programs have the author's uid associated with them, I was planning to use this to then retrieve the user's username from a separate node based off this uid value.
My Firebase set up:
"programs" : {
"-KYF3o3YD6F3FEXutuYH" : {
"content" : "Program Content Goes Here...",
"duration" : "4 Weeks",
"title" : "Chest Blast",
"type" : "Hypertrophy",
"uid" : "oLy9GOzDyKht7WWVZgpd3jPHxsE3"
},
"-KYF4ev88FQ2nEr6yTOW" : {
"content" : "Program Content Goes Here...",
"duration" : "6 Weeks",
"title" : "Full Back Workout",
"type" : "Strength",
"uid" : "oLy9GOzDyKht7WWVZgpd3jPHxsE3"
},
"-KZRYF9A8-8OHCNzOoPT" : {
"content" : "Eugene and Eamon",
"duration" : "4 Weeks",
"title" : "abc",
"type" : "abc",
"uid" : "oLy9GOzDyKht7WWVZgpd3jPHxsE3"
},
"-KbKNdrAcBarpaNoGf_e" : {
"content" : "Test",
"duration" : "test",
"title" : "New Test",
"type" : "test",
"uid" : "oLy9GOzDyKht7WWVZgpd3jPHxsE3"
},
"-KbKnXnyzj_EJp_wNw5y" : {
"content" : "1. Barbell Bench Press\n\nWhy it's on the list: You can generate the most power with barbell lifts, so the standard barbell bench allows you to move the most weight. It's also an easier lift to control than pressing with heavy dumbbells. The exercise is easy to spot and relatively easy to learn (if not master), There are plenty of bench-press programs you can follow to increase your strength.\n\n1. Barbell Bench Press\n\nWhy it's on the list: You can generate the most power with barbell lifts, so the standard barbell bench allows you to move the most weight. It's also an easier lift to control than pressing with heavy dumbbells. The exercise is easy to spot and relatively easy to learn (if not master), There are plenty of bench-press programs you can follow to increase your strength.",
"duration" : "1",
"title" : "1",
"type" : "1",
"uid" : "oLy9GOzDyKht7WWVZgpd3jPHxsE3"
}
},
"users" : {
"hds1WketiAUELVDOz1Dprvlu0KE3" : {
"username" : "Example"
},
"oLy9GOzDyKht7WWVZgpd3jPHxsE3" : {
"username" : "Test"
}
}
The table in the application looks like this:
As you can see, at the moment the author is just indicated by their uid. This was easy as I have it stored in the same place as all the other data being displayed but I need to use that uid value to search another node and grab the username associated with it and display it where the uid is shown now.
Below is the code I am using to retrieve and display the data. My main questions are:
1: What query can I use to search the users nodes for the matching uid and grab just the username of that person and display it in the table view?
2:Where should I place that query in the code? I was thinking to just place it in the func tableView() method as I could just search the user each time a new post is being added to the cell and that way I wouldn't have to make another NSMutable array to hold users who may not even have posts. Many thanks in advance for any help offered.
@IBOutlet weak var programsTableView: UITableView!
var programs = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
self.programsTableView.delegate = self
self.programsTableView.dataSource = self
//Call load data method to populate table with data from Firebase
loadData()
}
//Method to load data from Firebase
func loadData(){
FIRDatabase.database().reference().child("programs").observeSingleEvent(of: .value, with: { (snapshot) in
//Snapshot holds value and it is casted to NS Dictionary
if let programsDictionary = snapshot.value as? [String: AnyObject]{
for program in programsDictionary{
self.programs.add(program.value)
}
self.programsTableView.reloadData()
}
})
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! AllProgramsTableViewCell
// Configure the cell...
let program = self.programs[indexPath.row] as! [String: AnyObject]
//Populate row
//Grab title and add it to cell
cell.titleLabel.text = program["title"] as? String
//Grab type and add it to cell
cell.typeLabel.text = program["type"] as? String
//Grab duration and add it to cell
cell.durationLabel.text = program["duration"] as? String
//Grab content and add it to cell
cell.contentTextView.text = program["content"] as? String
//Grab author and add it to cell
cell.authorLabel.text = program["uid"] as? String
return cell
}