Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve randomly generated child ID from Firebase

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 = ""
                }
                })
        }
    }
like image 356
Michael Williams Avatar asked Mar 24 '16 21:03

Michael Williams


People also ask

How do I retrieve information from Firebase?

Asynchronous listeners: Data stored in a Firebase Realtime Database is retrieved by attaching an asynchronous listener to a database reference. The listener is triggered once for the initial state of the data and again anytime the data changes. An event listener may receive several different types of events.

What does getKey () do Firebase?

getKey() returns the key (last part of the path) of the location of the Snapshot. getReference() returns the Reference for the location that generated this Snapshot. getValue() returns the data contained in this Snapshot. hasChild() returns true if the specified child path has (non-null) data.


3 Answers

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

like image 163
Ron Harlev Avatar answered Nov 14 '22 12:11

Ron Harlev


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
    }
}
like image 28
Tim Vermeulen Avatar answered Nov 14 '22 11:11

Tim Vermeulen


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
like image 40
ttorbik Avatar answered Nov 14 '22 10:11

ttorbik