如何在 Firebase 中保存的数据下构建数据

How to Structure Data Under Saved Data in Firebase

这是我在 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];

当用户在应用程序上点击保存时,数据会保存到我的 Firebase 数据库中,但它会一直保存为新对象,就在之前保存的对象下方。我怎样才能让它保存一次,而保存的其余数据是第一个保存数据的子项?

这是一张图片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。我正在保存用户的坐标,但它一直在重新创建一个新的 'childByAutoId' 字符串,我只希望保存的数据属于第一个 'childByAutoId' 字符串,而不是继续创建新的 ID,因此每个用户的坐标都属于一个每次保存的ID。

更新: 我有不同的用户提交数据,其中数据是坐标。我希望使用 'childByAutoID' 仅在一个节点下创建相同的用户数据,其余数据(坐标)将在该节点下。它现在的设置方式是多个节点 (childByAutoId) 被记录到 Firebase,这让我很难阅读它,所以这就是为什么我希望首先创建一个节点 (childByAutoId),其余节点属于每个节点有人点击保存的时间。 这就是我目前的方式。 And this is how I want it

超级简单!

当用户进行身份验证时,auth Firebase 变量会填充他们的用户 ID。

一般来说,如果您要存储有关用户的信息,您会存储该数据或将该数据与他们的 uid 相关联。

所以,当你想为每个用户存储数据时,将其存储在父节点是他们的 uid 的节点中

Location_Coordinates
 LOCATION DATA
   uid_0
     childByAutoId
     childByAutoId
   uid_1
     childByAutoId
     childByAutoId

然后创建引用

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];