Reputation: 286
I can't figure how to identify which child from my Firebase database is being selected in the table view, and then update information under that selected child in the database.
I found an observe function that correctly displays all of the childByAutoID's for each row, but it shows every row, not the specific row which is selected and update the information under that child.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(tasks[indexPath.row].isFlagged)
var task = self.tasks[indexPath.row]
task.isFlagged.toggle()
guard let uid = Auth.auth().currentUser?.uid else { return }
var flag = ["isFlagged":false]
if tasks[indexPath.row].isFlagged == true {
flag = ["isFlagged":true]
} else {
flag = ["isFlagged":false]
}
Database.database().reference().child("users").child(uid).child("tasks").observeSingleEvent(of: .value, with: { (snapshot) in
if let result = snapshot.children.allObjects as? [DataSnapshot] {
for child in result {
var theKey = child.key as! String
print(theKey)
Database.database().reference().child("users").child(uid).child("tasks").child(theKey).updateChildValues(flag)
}
}
})
}
Upvotes: 0
Views: 40
Reputation: 100503
When you read tasks
you should store the root key of the dictionary to use it later
Then do this inside didSelectRowAt
let task = self.tasks[indexPath.row]
Database.database().reference().child("users").child(uid).child("tasks").child(task.key).updateChildValues(flag)
struct Task {
let key:String
.....
}
Upvotes: 2