Ege Kaan Gürkan
Ege Kaan Gürkan

Reputation: 2923

How to set more than one value to a child in Firebase using Swift?

I am trying to make a money related app which shows the user their spendings as a project to get used to Firebase. So I stumbled upon this issue; I can't seem to figure out how to add more than one expense assigned to a user. Whenever I add a new expense, the existing value in Firebase gets reset to the new value but I want it to store both the new and the old value. How can I do this?

Upvotes: 0

Views: 850

Answers (2)

Jay
Jay

Reputation: 35659

childByAutoId() is what you want for swift. See Updating Or Deleting specific data section in the Read and Write Data on iOS

let thisUserRef = ref.child("users").child(users uid)
let expenseRef = thisUserRef.child("expenses").childByAutoId()
let dict = ["expense_type": "travel", "expense_amt": "1.99"]
expenseRef.setValue(dict)

will results in

users
  uid_0
    -Y88jn90skda //<- the node key created with childByAutoId
       expense_type: "travel"
       expense_amt: "1.99"

the childByAutoId is super powerful and allows 'random' node keys to be generated that contain your child data.

See the answer to this question and this other question for some tips on data modeling and queries.

Upvotes: 0

chronikum
chronikum

Reputation: 736

There's something called "autoID" if that helps. I'll link that in a few moments.

Edit: It's used here.

Upvotes: 1

Related Questions