使用Firebase展平数据

时间:2016-03-08 01:23:06

标签: ios swift firebase

在这里阅读文档后: 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相同的ID。我怎么能这样做?

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

1 个答案:

答案 0 :(得分:1)

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

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

...

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