如何使用Swift正确观察Firebase的两个连接节点

时间:2017-05-28 18:34:13

标签: ios swift firebase firebase-realtime-database

这个问题的后续跟进:Join different nodes to observe them at once with 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
    }
}

我有两个主要问题:

  1. 首先,目前我不知道如何迭代一个特定的食谱,或者这是否是我想要达到的目标所必需的。现在我已经对recipeID进行了硬编码,以便能够在其中进行迭代。

  2. 使用此fetchRecipeIngredients()

  3. 时如何获取成分的详细信息

    提前致谢:)

1 个答案:

答案 0 :(得分:1)

你走了。

从Firebase结构开始

import * as SignatureCanvas from 'react-signature-canvas'

//... and use the `SignatureCanvas` class by accessing the `default property`
new SignatureCanvas.default()

那与你的相似

我们有一个主要功能来加载特定的配方,然后是一个加载成分的功能。

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

通过.value观察/ favorites节点(它将立即读取它们的所有收藏夹。迭代快照中的子节点。键将是配方节点名称(recipe_1和recipe_2)。然后你可以加载每个直接来自节点的配方名称(/ recipe_1 / name)并填充一个可用作tableView的数据源的数组