ios – 从Firebase中检索随机生成的子ID

我有Firebase为Swift应用程序中的记录生成随机密钥.如果我想更改特定记录,如何检索该密钥?在下面的示例中,我标记了一个任务完成,但是当我点击按钮时它不能按预期工作,因为我没有引用特定任务,但是为了引用任务,我需要随机生成的密钥.

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

我的Firebase结构:

>任务

>随机生成的ID

>标题:
>说明:

>随机生成的ID

>标题:
>说明:

更新1:

我已更新我的代码以获取所有任务的ID,但更新后端的功能无法正常工作.它只是让我们更新在应用程序中创建的任务.我使用Web Dashboard导入了150个任务.这些是我无法更新的.

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 = ""
                }
                })
        }
    }
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
    }
}
翻译自:https://stackoverflow.com/questions/36210424/retrieve-randomly-generated-child-id-from-firebase

转载注明原文:ios – 从Firebase中检索随机生成的子ID