Reputation: 121
I am using Firebase realtime database to store users lists of locations. At the moment instead of an item being added to the user list in firebase, its replaced. How do I get there to be an appended list which is created as a user adds locations into the tableview? So I would want the structure of the data to be:
USER_ID PlaceName: locationA locationB locationC
Is that possible to do? here is my code so far:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)
for place in storedPlaces {
storedNames.append(place.name)
}
cell.textLabel?.text = storedNames[indexPath.row]
let Pname: String = (cell.textLabel?.text!)!
print(Pname)
let PplaceID = "Hello"
let post : [String: AnyObject] = ["storedName" : Pname as AnyObject, "placeID" : PplaceID as AnyObject]
let databaseRef = FIRDatabase.database().reference()
databaseRef.child("PlaceNames").child((user?.uid)!).setValue(post)
return cell
Upvotes: 2
Views: 3271
Reputation: 35648
I think what you want is to let firebase define the location node keys:
let databaseRef = FIRDatabase.database().reference()
databaseRef.child("PlaceNames").child((user?.uid)!)
locationRef = databaseRef.childByAutoId()
locationRef.setValue(post)
This will result in a Firebase structure:
PlaceName
uid_0
-YUisisokapoksa
storedName: "some name"
placeID: "some id"
-YNisimaii990js
storedName: "some name"
placeID: "some id
There's some info on the Firebase Getting Started Guide about using childByAutoId - in the updating or deleting data section
Upvotes: 3