从Firebase中检索随机生成的儿童ID [英] Retrieve randomly generated child ID from Firebase

查看:104
本文介绍了从Firebase中检索随机生成的儿童ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  func doneHit(cell:TaskCell){
if let ip = tableView.indexPathForCell(cell){
var task = tasksInSectionArray [ip.section] [ip.row]
让tasksRef = ref.childByAppendingPath( 任务)
ref.observeSingleEventOfType(.value的,withBlock:在
{快照让doneRef = tasksRef。 childByAppendingPath(\(snapshot.key)/ done)
如果task.done == false {
task.done = true
cell.checkBox.image = UIImage(named: checkedbox)
doneRef.setValue(task.done)
}
else {
task.done = false
cell.checkBox.image = U IImage(命名为: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结构:


  1. 任务


    • 随机生成的ID


      • title:

      • 描述:


    • 随机生成的ID


      • title:

      • 描述:



更新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.children {
让_tasks =任务,FDataSnapshot
让ID = _tasks.key
print(id)
taskID = id
}
let doneRef = tasksRef.childByAppendingPath(\(taskID)/ done)
如果task.done == false {
task.done = true
cell.checkBox.image = UIImage(named:checkedbox)
doneRef.setValue(task.done)
}
其他{
task.done = false
cell.checkBox.image = UIImage(named:uncheckedbox)
doneRef.setValue(task.done)
}
让completedByRef = tasksRef.childByAppendingPath( \(TASKID)/ completedBy)
如果task.done == TRUE {
completedByRef.setValue(self.user)
cell.detailLabel.text =Completed By:\(self.user)
}
else {
completedByRef.setValue()
cell.detailLabel.text =





$ div class =h2_lin>解决方案

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

let id = task.key
//做其他事情
}
}


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

解决方案

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
    }
}

这篇关于从Firebase中检索随机生成的儿童ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆