【问题标题】:How to set more than one value to a child in Firebase using Swift?如何使用 Swift 在 Firebase 中为孩子设置多个值?
【发布时间】:2018-01-28 00:24:18
【问题描述】:

我正在尝试制作一个与金钱相关的应用程序,该应用程序向用户展示他们的支出,作为一个习惯 Firebase 的项目。所以我偶然发现了这个问题;我似乎无法弄清楚如何添加分配给用户的不止一项费用。每当我添加新费用时,Firebase 中的现有值都会重置为新值,但我希望它同时存储新值和旧值。我该怎么做?

【问题讨论】:

  • 请提供您到目前为止所做的工作。
  • 你使用了一个设定值,但你应该使用 updatechildvalues
  • 当我使用 updateChildValues 时,它会将用户 ID 放入;可选(“”)语句
  • 一般情况下,请提供代码和您的 Firebase 结构(请提供 TEXT,没有图片),以便我们根据您迄今为止的尝试制定一个好的答案。

标签: swift database firebase


【解决方案1】:

如果有帮助的话,有一种叫做“autoID”的东西。稍后我会链接它。

编辑:使用的是here

【讨论】:

    【解决方案2】:

    childByAutoId() 是你想要的 swift 。请参阅Read and Write Data on iOS中的更新或删除特定数据部分

    let thisUserRef = ref.child("users").child(users uid)
    let expenseRef = thisUserRef.child("expenses").childByAutoId()
    let dict = ["expense_type": "travel", "expense_amt": "1.99"]
    expenseRef.setValue(dict)
    

    将导致

    users
      uid_0
        -Y88jn90skda //<- the node key created with childByAutoId
           expense_type: "travel"
           expense_amt: "1.99"
    

    childByAutoId 非常强大,允许生成包含您的子数据的“随机”节点键。

    有关数据建模和查询的一些提示,请参阅this questionthis other question 的答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      • 2017-04-14
      相关资源
      最近更新 更多