我有一个问题,我正在尝试访问firebase上的一些数据,这是数据结构
我设法访问“最爱” 这是我以前得到的
ref.observeEventType(.Value, withBlock: { snapshot in
let fav = snapshot.value.objectForKey("favPost")
print("\(fav) Printed")
})
但我正在尝试访问“favPost”,但我无法理解它?!!
Firebase结构
{
"posts" : {
"-KGIxJybfQEJbcSy2bHH" : {
"author" : "Rioodi",
"postText" : "Test",
"votes" : 1
},
"-KGIxLUmPIa1Q1k0oRLs" : {
"author" : "Rioodi",
"postText" : "Raed",
"votes" : 0
},
"-KGJe-5ciAyu6Kom98E0" : {
"author" : "Neal",
"postText" : "Neal",
"votes" : 0
},
"-KGLFC0RqW_48lHMCsx8" : {
"author" : "Rioodi",
"postText" : "Test",
"votes" : 0
}
},
"users" : {
"afd0f27a-f62f-4cf1-9e81-032edc246687" : {
"email" : "test@test.com",
"favorite" : {
"-KGIxJybfQEJbcSy2bHH" : {
"favPost" : "Test"
}
},
"provider" : "password",
"username" : "Rioodi"
},
"fc56cc22-6275-48e0-8376-0b73b273b8e2" : {
"email" : "tests@test.com",
"provider" : "password",
"username" : "Neal"
}
}
}
答案 0 :(得分:2)
既然你知道userId并且你也知道你正在寻找自己喜欢的帖子,那就直接访问它吧。
let usersRef = self.myRootRef.childByAppendingPath("users")
let thisUserRef = usersRef.childByAppendingPath("this users id")
let thisUserFavoriteRef = thisUserRef.childByAppendingPath("favorite")
thisUserFavoriteRef.observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in
let fav = snapshot.value["favPost"] as! String
print(fav)
})
您可以将路径合并为一行
let thisUserFavoriteRef = rootRef.childByAppendingPath("users/this users id/favorite")
答案 1 :(得分:1)
您可以尝试以下代码。 (您可以根据要求进行更改。)
scope :seven_days, -> (boolean = true) { where( "start_date >= ?", Time.now - 7.days).where.not(price: 0).order("status DESC") }