2016-03-08 366 views
0

压扁数据在这里阅读文档后: https://www.firebase.com/docs/ios/guide/structuring-data.html与火力地堡

我对我的扁平化数据的工作。这是我面对此刻的问题,在下面的代码:

let firebaseDBNameUnit = ["titleString": "name1"], 
    firebaseNameRef = topRef.childByAppendingPath("MyApp/Name") 
    firebaseNameRef.childByAutoId().setValue(firebaseDBNameUnit) // * 
    let firebaseDBContentUnit = ["contentString": "very long content 1 11 111 1111 etc... blah blah blah"], 
    firebaseContentRef = topRef.childByAppendingPath("MyApp/Content") 
    // The following line is where my question is: 
    firebaseContentRef.childByAutoId().setValue(firebaseDBContentUnit) 

我想改变的最后一行上面代码中,使用一个ID是一样的中制作的标有*的行。我怎样才能做到这一点?

为了在名称和内容之间建立关系,我当然可以创建自己的字段,但使用Firebase提供的ID会更好。

回答

1

当您致电childByAutoId()时,它会返回对新位置的引用。如果你在一个变量中捕获它,你可以使用它:

let unitRef = firebaseNameRef.childByAutoId() 
unitRef.setValue(firebaseDBNameUnit) 

... 

firebaseContentRef.childByAppendingPath(unitRef.key).setValue(firebaseDBContentUnit) 
+0

完美的工作。非常感谢! – Michel

+0

如果我的回答很有帮助,请点击左侧的upvote按钮。如果它回答了您的问题,请单击旁边的复选标记以接受它。 –

+0

好的。我已经接受了答案,在阅读和测试之后(你没注意到吗?)。我认为这将做到这一切。无论如何,我也刚刚提出了它的意见。 – Michel