【问题标题】:unique property in FirebaseFirebase 中的独特属性
【发布时间】:2017-05-17 13:27:00
【问题描述】:

我有一个包含类别的 IOS 应用程序。我在 Firebase 上的存储如下所示:

-root
   -Categories
      -key
        - color:
        - name:
        - sum:
   -Expenses
      -key
        -amount: 
        -category: 
        -date: 
        -description: 
        -initiator: 
        -name: 

用户不得两次添加类别。我想让类别名称独一无二。可以在 Firebase 中进行吗? 提前致谢。

【问题讨论】:

    标签: ios firebase firebase-realtime-database unique-constraint


    【解决方案1】:

    无法确保 Firebase 数据库中的唯一值

    但另一方面,键在其上下文中始终是唯一的

    您可以利用它来对数据进行建模,以保证所需属性的唯一性。假设您希望类别名称是唯一的,请将类别存储在其名称下:

    -Categories
      -name
        - color:
        - sum:
    

    使用这种结构,您可以保证拥有唯一的类别名称。

    如果您必须将类别存储在其当前键下,但仍希望确保名称唯一。您可以创建一个二级索引,它使用类别名称作为键来确保它们的唯一性。

    -Categories
      -key
        - color:
        - name:
        - sum:
    -CategoryNames
      -name: key
    

    加藤对Enforcing unique usernames with Firebase simplelogin的回答中进一步解释了后一种方法

    【讨论】:

      【解决方案2】:

      您必须检查是否已经存在相同的类别。如果已经存在具有特定类别值的类别 (where clause),请不要添加它。关于如何使用 firebase 搜索特定结果,您可以查看此链接 https://stackoverflow.com/a/14965065/2246798

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-14
        • 1970-01-01
        • 2021-09-27
        • 1970-01-01
        • 2013-05-08
        • 1970-01-01
        相关资源
        最近更新 更多