2017-05-28 69 views
1

此問題跟從此:Join different nodes to observe them at once with firebase。我很抱歉,如果它非常相似,但我正努力在我的項目中解決它。如何使用Swift正確觀察兩個使用Firebase的加入節點

我有一個IngredientData其獲得幾種成分和一個屬性:

{ 
    "IngredientData": { 
    "-KkY92MUZNHXxg4ivGY7": { 
     "name": "Onions" 
    }, 
    "-KkY90e7dAgefc8zH3_F": { 
     "name": "Peas" 
    }, 
    "-KkY9-HWJANdfhmD-SJ-": { 
     "name": "Mushrooms" 
    }, 
    "-KkbWdomTYdpgEDLjC9w": { 
     "name": "Eggs" 
    }, 
    "-KkbXOECSCBNAcVaO9rp": { 
     "name": "Rice" 
    } 
    } 
} 

我然後有一個RecipeData包含配方和內部是成分

{ 
    "RecipeData": { 
    "-KlApwz5Y-TK_9z1sV1i": { 
     "recipeName": "Fried Rice", 
     "ingredients": { 
     "-KkbWdomTYdpgEDLjC9w": true, 
     "-KkbXOECSCBNAcVaO9rp": true, 
     "-KkY90e7dAgefc8zH3_F": true 
     } 
    } 
    } 
} 

我現在想進去配方成分的細節,所以我可以得到財產。我有一個函數,配方的成分裏迭代:

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) 
     } 
    }) 
} 

這裏是我Recipe類:

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 
    } 
} 

我有2個主要問題:

  1. 首先,在我不知道如何通過一個特定的配方迭代,或者如果這對我想實現的目標是必要的。現在我已經對recipeID進行了硬編碼,以便能夠在其中進行迭代。

  2. 提前使用該fetchRecipeIngredients()

感謝時,我如何獲得成分的細節:)

回答

1

在這裏你去。

與火力地堡結構

{ 
    "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" 
    } 
    } 
} 

這類似於你

開始我們有一個主要功能在特定的配方加載,然後加載成分的功能。

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)") 
      }) 
     } 
    } 
} 

和輸出

Pizza 
    Onions 
    Mushrooms 
    Tomatoes 
    Dough 

編輯

在回答跟進評論:

如果用戶希望存儲自己喜愛的,然後創建一個節點

favorites 
    recipe_1: true 
    recipe_2: true 

通過.value觀察/ favorites節點(它將一次讀取所有的收藏夾。在快照中迭代孩子。這些鍵將是配方節點名稱(recipe_1和recipe_2)。然後,您可以直接從節點加載每個配方名稱(/ recipe_1/name)並填充一個可用作tableView數據源的數組。

+0

非常感謝您的回答,它完美地工作。關於識別特定配方只是一個問題。目前,我必須在'recipieToLoadRef'中硬編碼_ID_以獲得配方的_reredients_的詳細信息。然而,假設我想爲用戶創建一個'favouriteList'來添加配方,我將如何指定要使用的配方,然後顯示類似於tableView的東西?我嘗試過使用與此相同的方法。如果這個問題不相關,那麼不要擔心。再次感謝:) – Chace

+0

@Chace很高興幫助!我更新了一些附加信息的答案,以解決您的後續問題。 – Jay

+0

我很感激這個答案。我已經嘗試過這一點,並且能夠在用戶收藏夾內打印出食譜。然而,_You然後可以直接從節點加載每個配方名稱的部分(/ recipe_1/name)並填充一個可用作tableView_的數據源的數組,這就是我被卡在的地方:/ – Chace

相關問題