I am trying to update one of the parents in my database. However, I am unsuccessful. Here is what the DB looks like:
I am able to Update
the description and due date like so:
taskRef = Database.database().reference(withPath: "Tasks").child(titleOfTask)
taskRef?.updateChildValues(["Due Date": date_time])
taskRef?.updateChildValues(["Description": taskDescription])
However, I am trying to figure out how to update the name of the task. For example, if the user is trying to update the description of task "Alpha" and also decides to rename the task as well, how can I update the name?
I first tried to do this, but it didn't work, it just created and another key, value pair under the "Alpha" parent.
newTaskRef?.updateChildValues([titleOfTask: taskTitle])
Then I realized that my database reference is to the Task Title already, so it wouldn't have worked. Then I thought about creating another database reference to just "Tasks", and updating the title like so:
newTaskRef = Database.database().reference(withPath: "Tasks")
newTaskRef?.updateChildValues([titleOfTask: taskTitle])
But this didn't work either. Not sure what else I could try, or where I am going wrong.
Important Things:
- taskTitle holds the new input the user enters
- titleOfTask is the old name of the task. (i.e, used as a reference to read from DB)
in other words:
If the user wants to edit the "Alpha
" Task, we would have to pass the STRING "Alpha" to the database.reference
so we can read its values and update the description and/or due date. So the original Title of Task is stored in "titleOfTask", and the new task title would be stored in "taskTitle"
CodePudding user response:
You can't directly change the keys in the DB. Instead you can follow a 2 step approach:
- Create a new key with the updated title and info.
- Delete the old key from the DB