2016-03-08 369 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