BrandonMayU
BrandonMayU

Reputation: 716

Set value in Firebase database by only overwriting some data Swift 3

Image of Current Database

The image above shows how my database is setup, where the folder name is the user name and nested inside, is a value that is called "userName" containing their name again.

When a user checks in I am uploading the "hour" and "minute" however I also need to reupload all the other people with their file being there username and then nested inside there "userName".

To do this I use

let arrayData = ["userName" : "\(userName!)"]
//UploadArrayPath stores the data in (15-3-2017 Block 2) as seen in the photo above)
uploadArrayPath.setValue(arrayData)

Everything works and the data goes to the right place, however, all the data gets overwritten and erases the time that already exists for other users. Is there any way to just overwrite the username of everyone but not the time of other people?

Upvotes: 0

Views: 1250

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

See the example in the Firebase documentation on updating specific fields:

let key = ref.child("posts").childByAutoId().key
let post = ["uid": userID,
            "author": username,
            "title": title,
            "body": body]
let childUpdates = ["/posts/\(key)": post,
                    "/user-posts/\(userID)/\(key)/": post]
ref.updateChildValues(childUpdates)

Although in this case, it seems you could also just do:

uploadArrayPath.child(userName).child("userName").setValue(userName)

Btw: I'm not sure why you duplicate the user's name like this. Renaming a user is going to get quite confusing with their name being both a value and the key of the node.

Upvotes: 1

Related Questions