Retrieve randomly generated child ID from Firebase

匿名 (未验证) 提交于 2019-12-03 02:50:02

问题:

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:

  1. tasks
    • randomly generated ID
      • title:
      • description:
    • randomly generated ID
      • title:
      • description:

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 = ""                 }                 })         }     } 

回答1:

tasksRef.observeSingleEventOfType(.Value, withBlock: { snapshot in     for task in snapshot.children {         guard let taskSnapshot = task as? FDataSnapshot else {             continue         }          let id = task.key         // do other things     } } 


回答2:

To create a randomly generated key you need to use childByAutoId() (which I can't see in your example code).
This will return a Firebase reference you could use and which will return the key with it's .key property

var post1Ref = ref.childByAutoId() post1Ref.setValue(post1)  var postId = post1Ref.key 

See documentation here



回答3:

Following Tim's answer for Swift 3 and Firebase 4 I had to use the below

for task in snapshot.children {             guard let taskSnapshot = task as? DataSnapshot else {                 continue             }              let id = taskSnapshot.key 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!