问题
I am struggling with writing data to Firebase in the desired way.
The situation: I want to store multiple "trips" for each user. Every "trip" contains locations with lat, long, altitude and a timestamp. On certain conditions, I want to add a new location to the most recent "trip".
Data Structure is the following:
- root
-- users
--- userID
---- trips
------ trip1
------- location
------ trip2
------- location
What would be the best way to add locations to the most recent trip (trip2 in this case)?
I had hoped something similar to this is possible:
func setupFirebase() {
ref = FIRDatabase.database().reference()
userRef = ref.child("users")
userID = FIRAuth.auth()?.currentUser?.uid
tripsRef = self.userRef.child(userID!).child("trips")
currentTripRef = tripsRef.queryLimited(toLast: 1)
}
and then I could simply go and:
currentTripRef.child(dateString).setValue([
"timestamp": dateString,
"lat": lat,
"lng": lng,
"alt": alt
])
wherever I need to write something. This obviously is not the right way to do it, but I can't seem to figure out what the correct approach is.
I figured I could observe my tripsRef, get an array of keys and inside of that observing function write to the child ref with the last key. But that doesn't sound efficient at all.
Any solution on this? Thanks in advance!
回答1:
First, remember it's best practice to disassociate your node names from you data. You may be doing that but wanted to stress that point.
Here's a proposed structure that handles users and the their trips
root
users
uid_0
name: "Jamie"
uid_1
name: "Adam"
trips
-Yijaisioaooisd
user_id: "uid_0"
trip_number: 1
trip_name: "France"
locations
-Yuijiaissso
location_name: "Bordeaux"
stop_number: 1
-Yijs9ojnas9
location_name: "Burgundy"
stop_number: 2
-Yijispoais
location_name: "Châteauneuf-du-Pape"
stop_number: 3
If you wanted to add another stop/location to uid_0's trip to France, the ref is
let locationsRef = rootRef.child("trips").child("-Yijaisioaooisd").child("locations")
and then
let anotherLocationRef = locationsRef.childByAutoId()
let stopDict = ["location_name": "Médoc", "stop_number": 4]
locationsRef.setValue(stopDict)
That being said, there may be situations where you want to denormalize your data and break the locations out (for queries etc)
So that would be
locations
trip_id: "-Yijaisioaooisd"
locations:
-Yuijiaissso
location_name: "Bordeaux"
stop_number: 1
-Yijs9ojnas9
location_name: "Burgundy"
stop_number: 2
-Yijispoais
location_name: "Châteauneuf-du-Pape"
stop_number: 3
based on a comment, adding some additional info:
If you want to add another stop to uid_0's latest trip there are several options and the actual answer would depend on the structure of the data.
Using queryLimitedToLast, the last stop in the locations node could be queried to get the stop number (3 in the above example) to where you can increment it and then write out the next stop. This would work if you know the parent key -Yijaisioaooisd as the ref would be -Yijaisioaooisd/locations.
If you don't know the parent key you can easily obtain it with a query on the trips node for user_id equal to uid_0. That will return a snapshot. Then snapshot.key would provide the -Yijaisioaooisd parent key.
来源:https://stackoverflow.com/questions/41333862/how-to-write-to-the-last-child-of-a-firebase-ref-in-swift