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

时间:2016-03-24 21:38:33

标签: ios swift firebase

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

 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结构:

  1. 任务
    • 随机生成的ID
      • 标题
      • 描述:
    • 随机生成的ID
      • 标题
      • 描述:
  2. 更新1:

    我已更新我的代码以获取所有任务的ID,但更新后端的功能无法正常工作。它只是让我们更新在应用程序中创建的任务。我使用Web仪表板导入了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 = ""
                    }
                    })
            }
        }
    

4 个答案:

答案 0 :(得分:23)

要创建随机生成的密钥,您需要使用childByAutoId()(我在示例代码中看不到)。
这将返回您可以使用的Firebase参考,并将使用.key属性返回密钥

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

var postId = post1Ref.key

请参阅文档here

答案 1 :(得分:5)

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 :(得分:1)

根据Tim对Swift 3和Firebase 4的回答,我不得不使用下面的

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

            let id = taskSnapshot.key

答案 3 :(得分:0)

Ron 答案的替代方法是先生成密钥,然后使用它来设置值:

let autoId = databaseReference.childByAutoId().key
databaseReference.child(autoId).setValue(YOUR_VALUE)