I have Firebase generating random keys for my records in a Swift app. How do I go about retrieving that key if I want to change a specific record? In my example below, I'm marking a task complete but when I hit the button it doesn't work as intended because I am not referencing the particular task but to reference the task I need the randomly generated key.
func doneHit(cell:TaskCell) { if let ip = tableView.indexPathForCell(cell) { var task = tasksInSectionArray[ip.section][ip.row] let tasksRef = ref.childByAppendingPath("tasks") ref.observeSingleEventOfType(.Value, withBlock: { snapshot in let doneRef = tasksRef.childByAppendingPath("\(snapshot.key)/done") if task.done == false { task.done = true cell.checkBox.image = UIImage(named: "checkedbox") doneRef.setValue(task.done) } else { task.done = false cell.checkBox.image = UIImage(named: "uncheckedbox") doneRef.setValue(task.done) } let completedByRef = tasksRef.childByAppendingPath("\(snapshot.key)/completedBy") if task.done == true { completedByRef.setValue(self.user) cell.detailLabel.text = "Completed By: \(self.user)" } else { completedByRef.setValue("") cell.detailLabel.text = "" } }) } }
My Firebase structure:
- tasks
- randomly generated ID
- title:
- description:
- randomly generated ID
- title:
- description:
- randomly generated ID
Update 1:
I have updated my code to get the IDs for all of the tasks but the functionality of updating the backend isn't working properly. It is only letting update the tasks created in the app. There are 150 tasks that I imported using the web Dashboard. Those are the ones I can't get to update.
func doneHit(cell:TaskCell) { if let ip = tableView.indexPathForCell(cell) { var task = tasksInSectionArray[ip.section][ip.row] let tasksRef = ref.childByAppendingPath("tasks") var taskID = "" tasksRef.observeSingleEventOfType(.Value, withBlock: { snapshot in for task in snapshot.children { let _tasks = task as! FDataSnapshot let id = _tasks.key print(id) taskID = id } let doneRef = tasksRef.childByAppendingPath("\(taskID)/done") if task.done == false { task.done = true cell.checkBox.image = UIImage(named: "checkedbox") doneRef.setValue(task.done) } else { task.done = false cell.checkBox.image = UIImage(named: "uncheckedbox") doneRef.setValue(task.done) } let completedByRef = tasksRef.childByAppendingPath("\(taskID)/completedBy") if task.done == true { completedByRef.setValue(self.user) cell.detailLabel.text = "Completed By: \(self.user)" } else { completedByRef.setValue("") cell.detailLabel.text = "" } }) } }