【问题标题】:firebase swift -- users queryfirebase swift -- 用户查询
【发布时间】:2016-10-14 02:58:45
【问题描述】:

我正在尝试找到一种方法来查询我的应用程序中的用户。例如,如果我的用户想搜索一个用户以将他添加为朋友。我想我可以将所有用户保存在 Firebase 数据库中,然后在数据库中执行查询,但它看起来非常低效(并且必须有一个更简单的内置方法来做到这一点)。

【问题讨论】:

  • 为什么效率低下?如果您有一个用户列表并且想要执行搜索,则必须由某人进行查询。魔术师不存在:D
  • 你总能猜到,如果你每次都是正确的,那将是 O(1)
  • 我的问题是是否有用户查询,例如在 parse 中您可以创建查询以查找用户(使用 id、用户名或电子邮件)的查询。 Firebase中是否存在这个,还是我需要把所有用户放到实时数据库中,然后在这个数据库中查询。
  • 这是 firebase 中的基本查询。请记住,Firebase 中没有这样的“用户”。如果您想保留用户名、电子邮件等,您可以将它们保留在 /users 节点中,然后在该节点上查询您要查找的特定用户。
  • Firebase 身份验证中没有内置 API 来查询用户。这就是为什么许多开发人员还将有关其用户的信息存储在他们的 Firebase 数据库中的原因。请参阅stackoverflow.com/questions/37449635/…stackoverflow.com/questions/33737009/…stackoverflow.com/questions/14673708/…

标签: swift firebase firebase-realtime-database firebase-authentication


【解决方案1】:

“.indexOn”是你的解决方案!

如果您像下面这样构建您的日期,您将能够查询用户!在这里,您可以在 full_name 和 reversed_full_name 上查询用户。您可以提供要查询的任何孩子。例如,您可以在人员下添加电子邮件字段,然后在“.indexOn”中添加“电子邮件”!!!

"people": {
  ".indexOn": ["_search_index/full_name", "_search_index/reversed_full_name"],
  ".read": true,
  "$uid": {
    ".write": "auth.uid === $uid",
    "full_name": {
      ".validate": "newData.isString()"
    },
    "profile_picture": {
      ".validate": "newData.isString()"
    },
    "posts": {
      "$postId": {
        ".validate": "newData.val() === true && newData.parent().parent().parent().parent().child('posts').child($postId).exists()"
      }
    },
    "_search_index": {
      "full_name": {
        ".validate": "newData.isString()"
      },
      "reversed_full_name": {
        ".validate": "newData.isString()"
      }
    },
    "following": {
      "$followedUid": {
        ".validate": "newData.parent().parent().parent().parent().child('followers').child($followedUid).child($uid).val() === true" // Makes sure /followers is in sync
      }
    }
  }

希望这会有所帮助。这是整个安全规则的链接,由 Firebase https://github.com/firebase/friendlypix/blob/master/web/database-rules.json 提供

【讨论】:

    猜你喜欢
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 2021-10-22
    • 2018-04-01
    • 2017-05-06
    • 1970-01-01
    • 2016-06-25
    相关资源
    最近更新 更多