Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase overwriting entries

I am new to Firebase and not sure how to best explain this but I will try.

I am trying to have my app create an entry for each user. Then each user entry has multiple (0 through n) sub-entries where each sub-entry is a simple string. Basically there is a user-id (the main entry) and their tasks are the sub-entries.

Now my problem is whenever I push data (the sub-entries) to the main entries, all of the previous sub-entries are deleted and only the most recent one is pushed. I have been looking through the documentation and Googling like crazy but nothing seems to work.

I have tried this:

@IBAction func testWrite(sender: AnyObject) {
    let def = NSUserDefaults.standardUserDefaults()
    let uid = def.valueForKey("uid")
    let root = Firebase(url: getFirebaseURL())

    let text = self.tempText.text!
    let dataRef = root.childByAppendingPath(uid as! String)
    let data = ["test": String(text)]
    dataRef.setValue(data)
}

Which appends to the user-id entry fine, with a key of "test" and a value of the 'text'

So then I kill the app and change it to:

@IBAction func testWrite(sender: AnyObject) {
    let def = NSUserDefaults.standardUserDefaults()
    let uid = def.valueForKey("uid")
    let root = Firebase(url: getFirebaseURL())

    let text = self.tempText.text!
    let dataRef = root.childByAppendingPath(uid as! String)
    let data = ["CHANGED": String(text)]
    dataRef.setValue(data)
}

And it pushes fine, but then the previous entry was just deleted and the only entry left is this one.

What I am trying to do is maybe incrementally (having a numbered key possibly?) add items one by one without having other entries deleted.

I hope this makes sense :P and any help is greatly appreciated!

like image 242
dsch Avatar asked Feb 08 '23 09:02

dsch


2 Answers

What is happening here is, you are setting the entire branch (Users/UserID##), to a value, and that value is a single node Changed:<somestring>

Conceptually, it may help to think of the key you want to set as being just another branch e.g (Users/UserID##/TaskID##)

So conceptually, instead of approaching it like this:
Users/UserID = Key:Value

Approach it like this:
Users/UserID/Key = Value

Note: the branch Users/UserID/Key does not have to exist prior to you assigning it a value.


e.g you could change your reference to point at the subkey you want to add or change:

let dataRef = root.childByAppendingPath(uid as! String + "/Task001")
dataref.setValue(String(text))

I concur that what you are doing is a great way to start learning Firebase, and how it works. But once you get going, instead of generating and using your own key for your list of subtasks, do look into childByAutoId, it will automatically create the subkeys for you, plus much more, and is much easier to manage and code.

Documentation Here

Edit: Suggest referring to Frank's better answer below.

like image 54
Mtl Dev Avatar answered Feb 13 '23 07:02

Mtl Dev


An alternative to @MtlDev's answer would be to use updateChildValues():

let data = ["CHANGED": String(text)]
dataRef.updateChildValues(data)

While setValue() replaces the current data with the new value, updateChildValues() updates it in place.

See the Firebase documentation on updating saved data.

like image 40
Frank van Puffelen Avatar answered Feb 13 '23 06:02

Frank van Puffelen