如何使用Swift正确观察两个加入的Firebase节点 [英] How to properly observe two joined nodes with Firebase using Swift
问题描述
我有一个 IngredientData 它获得几个成分和property:
{
IngredientData:{
-KkY92MUZNHXxg4ivGY7:{
name:洋葱
},
-KkY90e7dAgefc8zH3_F:{
name:Peas
},
-KkY9-HWJANdfhmD- SJ-:{
name:Mushrooms
},
-KkbWdomTYdpgEDLjC9w:{
name:Eggs
},
-KkbXOECSCBNAcVaO9rp:{
name:Rice
}
}
}
然后我有一个包含 recipe 和 的
{
RecipeData:{
-KlApwz5Y-TK_9z1sV1i:{
recipeName:炒饭,
成分:{
-KkbWdomTYdpgEDLjC9w:true,
-KkbXOECSCBNAcVaO9rp: true,
-KkY90e7dAgefc8zH3_F:true
}
}
}
}
现在我正在尝试获取食谱中配料的详细信息,以便获取名称属性。我有一个函数在配方的内部进行迭代:
pre $ func fetchRecipeIngredients()
let databaseRef ():= FIRDatabase.database()。reference()。child(RecipeData / recipe / -KlAwp5Y-TK_9z1sV1i / ingredients)
databaseRef.observeSingleEvent(of:FIRDataEventType.value,with:{snapshot in
for snapshots.children {
print(snapshot)
}
})
}
这是我的食谱
类:
class Recipe {
var name:String!
var key:String
init(从快照:FIRDataSnapshot){
let snapshotValue = snapshot.value as! [字符串:任何]
self.name = snapshotValue [recipeName] as! String
self.key = snapshot.key
}
}
两个主要问题:
-
首先,目前我不知道如何迭代一个特定的配方,或者如果这是我想要实现的必要条件。现在我已经对recipeID进行了硬编码,以便能够在其中进行迭代。
在此先感谢:)
从Firebase结构开始
{
IngredientData:{
ingredient_0:{
name:洋葱
},
ingredient_1:{
name:Peas
},
ingredient_2:{
name:蘑菇
},
ingredient_3:{
name:番茄
},
ingredient_4:{
name:面团
}
},
RecipeData:{
recipe_0:{
ingredients:{
ingredient_0:true,
ingredient_1:true,
ingredient_2 :真
$ name b
$ b $ {
ingredient_0:true,
ingredient_2:true,
ingredient_3:true,
ingredient_4:true
},
name:Pizza
}
}
}
这与你的相似
我们有一个主要的功能来加载一个特定的配方,然后一个函数来加载成分。
func loadRecipe(){
let recipiesRef = self.ref.child(RecipeData)
let recipieToLoadRef = recipiesRef.child( recipe_1)
recipieToLoadRef.observeSingleEvent(of:.value,with:{snapshot in
let recipieDict = snapshot.value as! [String:Any]
让name = recipieDict [name] as!字符串
让ingredientsSnap = snapshot.childSnapshot(forPath:ingredients)
print(name)
self.loadIngredients(ingredientSnap:ingredientsSnap)
})
}
调料成分(ingredientSnap:DataSnapshot){
让ingredientsRef = self.ref.child(IngredientData)
用于配料中的成分添加成分.snap.children {
让snap =成分为! DataSnapshot
let ingredientKey = snap.key
let thisIngredient = ingredientsRef.child(ingredientKey)
thisIngredient.observeSingleEvent(of:.value,with:{snapshot in
let ingredientDict = snapshot .value as![String:Any]
let ingredientName = ingredientDict [name] as!String
print(\(memberName))
})
}
$ b $和$输出
比萨
洋葱
蘑菇
西红柿
面团
编辑
回应后续评论:
如果您的用户想要存储他们的收藏夹,那么创建一个节点
收藏夹
recipe_1:true
recipe_2:true
观察/ favorites节点。价值(这将读取他们所有的青睐在一次。迭代快照中的孩子。密钥将是配方节点名称(recipe_1和recipe_2)。您可以直接从节点加载每个配方名称(/ recipe_1 / name),然后填充一个可用作tableView数据源的数组。
This question follows up from this: Join different nodes to observe them at once with firebase. I apologise if it is very similar but I am struggling to solve it in my project.
I have an IngredientData which obtains several ingredients and a property:
{
"IngredientData": {
"-KkY92MUZNHXxg4ivGY7": {
"name": "Onions"
},
"-KkY90e7dAgefc8zH3_F": {
"name": "Peas"
},
"-KkY9-HWJANdfhmD-SJ-": {
"name": "Mushrooms"
},
"-KkbWdomTYdpgEDLjC9w": {
"name": "Eggs"
},
"-KkbXOECSCBNAcVaO9rp": {
"name": "Rice"
}
}
}
I then have a RecipeData that contains recipe and inside that are ingredients.
{
"RecipeData": {
"-KlApwz5Y-TK_9z1sV1i": {
"recipeName": "Fried Rice",
"ingredients": {
"-KkbWdomTYdpgEDLjC9w": true,
"-KkbXOECSCBNAcVaO9rp": true,
"-KkY90e7dAgefc8zH3_F": true
}
}
}
}
I am now trying to get the details of the ingredients inside recipe so I can get the name property. I have a function which iterates inside the recipe's ingredient:
func fetchRecipeIngredients() {
let databaseRef = FIRDatabase.database().reference().child("RecipeData/recipe/-KlApwz5Y-TK_9z1sV1i/ingredients")
databaseRef.observeSingleEvent(of: FIRDataEventType.value, with: { snapshot in
for ingredients in snapshot.children {
print(snapshot)
}
})
}
Here is my Recipe
class:
class Recipe {
var name: String!
var key: String
init(from snapshot: FIRDataSnapshot) {
let snapshotValue = snapshot.value as! [String: Any]
self.name = snapshotValue["recipeName"] as! String
self.key = snapshot.key
}
}
I have two main issues:
Firstly, at the moment I am not sure how to iterate through a specific recipe or whether if that's necessary for what I want to achieve. For now I have hard-coded the recipeID to be able iterate inside it.
How do I get the details of the ingredients when using this fetchRecipeIngredients()
Thanks in advance :)
解决方案 Here you go.
Starting with a Firebase Structure
{
"IngredientData" : {
"ingredient_0" : {
"name" : "Onions"
},
"ingredient_1" : {
"name" : "Peas"
},
"ingredient_2" : {
"name" : "Mushrooms"
},
"ingredient_3" : {
"name" : "Tomatoes"
},
"ingredient_4" : {
"name" : "Dough"
}
},
"RecipeData" : {
"recipe_0" : {
"ingredients" : {
"ingredient_0" : true,
"ingredient_1" : true,
"ingredient_2" : true
},
"name" : "Fried Rice"
},
"recipe_1" : {
"ingredients" : {
"ingredient_0" : true,
"ingredient_2" : true,
"ingredient_3" : true,
"ingredient_4" : true
},
"name" : "Pizza"
}
}
}
That's similar to yours
And we have a main function to load in a specific recipe, and then a function to load the ingredients.
func loadRecipe() {
let recipiesRef = self.ref.child("RecipeData")
let recipieToLoadRef = recipiesRef.child("recipe_1")
recipieToLoadRef.observeSingleEvent(of: .value, with: { snapshot in
let recipieDict = snapshot.value as! [String:Any]
let name = recipieDict["name"] as! String
let ingredientsSnap = snapshot.childSnapshot(forPath: "ingredients")
print(name)
self.loadIngredients(ingredientSnap: ingredientsSnap)
})
}
func loadIngredients(ingredientSnap: DataSnapshot) {
let ingredientsRef = self.ref.child("IngredientData")
for ingredient in ingredientSnap.children {
let snap = ingredient as! DataSnapshot
let ingredientKey = snap.key
let thisIngredient = ingredientsRef.child(ingredientKey)
thisIngredient.observeSingleEvent(of: .value, with: { snapshot in
let ingredientDict = snapshot.value as! [String: Any]
let ingredientName = ingredientDict["name"] as! String
print(" \(ingredientName)")
})
}
}
}
and the output
Pizza
Onions
Mushrooms
Tomatoes
Dough
EDIT
In response to a follow up comment:
If your users want to store their favorites, then create a node
favorites
recipe_1: true
recipe_2: true
Observe the /favorites node by .value (which will read all of their favorites in at once. Iterate over the children in the snapshot. The keys will be the recipe node names (recipe_1 and recipe_2). You can then load each recipe name from the node directly (/recipe_1/name) and populate an array that you can use as the datasource for a tableView
这篇关于如何使用Swift正确观察两个加入的Firebase节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!