Xcode Objc Firebase数据库将多个子节点添加到节点

时间:2017-09-28 05:23:23

标签: objective-c database xcode firebase firebase-realtime-database

我正在尝试将子节点和子节点添加到我的firebase数据库,以便我的数据库看起来像这样:

{    
    "users": {
        "user.uid": {
            "userEmail":"blah@gmail.com.com",
            "project1": {
                "projectName":"1700 Broadway"
            }
        }
    }
}

我的代码如下所示:

//create a root ref
rootRef = [[FIRDatabase database] reference];

//create a child of the rootRef
FIRDatabaseReference *usersRef = [rootRef child:@"users"];               

//Create a child under the users ref
FIRDatabaseReference *newUserRef = [usersRef child:user.uid];

//set a value of the dictionary for which UID is the key
[newUserRef setValue:@{@"userEmail":emailString}];

//For some reason this reference isn't being created in FB
//Create a child under the users ref
FIRDatabaseReference *projectRef = [usersRef child:@"project1"];

//set a value of the dictionary for which project1 is the key
[projectRef setValue:@{@"projectName":projectName}]; 

但是,我在firebase中的数据库如下:

{    
    "users": {
        "user.uid": {
            "userEmail":"blah@gmail.com.com",
        }
    }
}

在控制台上看起来像这样:

mastertracker-9cf47addclose
    users
        L0gEKh4f0WctbioRddiziRpfwgC3
            userEmail: "testemail@gmail.com"

任何人都可以帮助解释为什么" project1"在用户id节点下没有引用child,这应该使数据看起来像这样?

mastertracker-9cf47addclose
    users
        L0gEKh4f0WctbioRddiziRpfwgC3
            userEmail: "testemail@gmail.com"
            project1
                projectName: "test project"

谢谢,

2 个答案:

答案 0 :(得分:0)

您在创建时已初始化newUserRef但未指定键。 我不能在Objective中给出代码,我使用Swift 3,但你应该找到一种方法来指定密钥,否则它会生成一个带有自动计算密钥的位置。

child("key").set(value).

答案 1 :(得分:0)

由于您要将project1作为兄弟userEmail添加,因此您应该使用

 FIRDatabaseReference *projectRef = [newUserRef child:@"project1"]; 

而不是

FIRDatabaseReference *projectRef = [usersRef child:@"project1"];