Reputation: 27
This is my code in Firebase.
//Create a reference to a Firebase database URL
Firebase *savedRef2 = [[Firebase alloc] initWithUrl:@"https://example.firebaseio.com/Location_Coordinates"];
// Write data to Firebase
Firebase *postRef2 = [savedRef2 childByAppendingPath: @"LOCATION DATA"];
NSDictionary *post1 = @{
@"DATE": dateString,
@"Lat": @(location.coordinate.latitude),
@"Long": @(location.coordinate.longitude),
@"USER": name,
};
Firebase *post1Ref = [postRef2 childByAutoId];
[post1Ref setValue: post1];
When a user clicks save on the app, the data is saved to my Firebase data base but it keeps saving as a new object right under the previous saved. How can I make it so it saves once and the rest that are saved are childs of that first saved data?
This is a picture Current saved data for coordinates. of how it keeps saving and I don't want it like this. How can I make it so it looks like this 2. I am saving coordinates of users but it keeps recreating a new 'childByAutoId' string and I just want the saved data to fall under the 1st 'childByAutoId' string and not to keep creating new ID's so each users coordinates fall under one ID each time it is saved.
UPDATE:
I have different users submitting data and in that data are coordinates. I want that same users data to be created ONLY under ONE node using 'childByAutoID' and the rest of the data (coordinates) will be under that. The way it is set up right now is that MULTIPLE nodes (childByAutoId) are being logged to Firebase and it makes it hard for me to read it so that is why I want ONE node (childByAutoId) created first and the rest fall under that EACH time someone clicks save.
This is how I currently have it. And this is how I want it
Upvotes: 0
Views: 854
Reputation: 35659
Super easy!
When a user authenticates the auth Firebase variable is populated with their user id.
In general if you are storing information about users you would store that data or associate that data with their uid.
So, when you want to store data for each user, store it in a node where the parent is their uid
Location_Coordinates
LOCATION DATA
uid_0
childByAutoId
childByAutoId
uid_1
childByAutoId
childByAutoId
then to create the reference
Firebase *rootRef = [[Firebase alloc] initWithUrl:@"https://example.firebaseio.com];
Firebase *locCoordsRef = [rootRef childByAppendingPath("Location_Coordinates");
Firebase *locDataRef = [locCoordsRef childByAppendingPath("LOCATION DATA"];
NSString *uid = rootRef.authData.uid
Firebase *thisUserRef = [locDataRef childByAppendingPath(uid)];
NSDictionary *userDataDict = @{
@"DATE": dateString,
@"Lat": @(location.coordinate.latitude),
@"Long": @(location.coordinate.longitude),
@"USER": name,
};
Firebase *dataRef = [thisUserRef childByAutoId];
[dataRef setValue: userDataDict];
Upvotes: 1