【问题标题】:Querying for specific strings in Firebase?在 Firebase 中查询特定字符串?
【发布时间】:2016-06-18 12:13:59
【问题描述】:

我有一个看起来像这样的数据库:

  "trucks" : {
    "3705ec54-8a2e-4eb1-8bb9-ab2243645ac1" : {
      "email" : "sandwiches@123.com",
      "name" : "Sandwich Truck",
      "phone" : "123 - 456 - 1234",
      "provider" : "password",
      "selfDescription" : "We serve delicious sandwiches at a moderate price.  Cards Accepted.",
      "userType" : "truck",
      "website" : "www.sandwiches.com"
    },
    "54fea8cd-2203-46bd-aaf8-9d823e85313d" : {
      "email" : "pizza@123.com",
      "name" : "Supa Pizza",
      "phone" : "619 - 222 - 4444",
      "provider" : "password",
      "selfDescription" : "We serve incredible pizza at an incredibly unfair price.",
      "userType" : "truck",
      "website" : ""
    },
    "6c542367-507c-4d01-af2c-bf93a7efaef4" : {
      "email" : "fries@123.com",
      "name" : "Pete's Fries",
      "phone" : "11111111111111111111",
      "profilePhoto" : "",
      "provider" : "password",
      "selfDescription" : "We make some of the world's most delicious fries.",
      "userType" : "truck",
      "website" : ""
    },
    "7c6c4395-aec1-443c-908d-62db517def5e" : {
      "email" : "chili@123.com",
      "name" : "Mark's Chili",
      "phone" : "1-800-CHIL-LLL",
      "profilePhoto" : "",
      "provider" : "password",
      "selfDescription" : "We serve the most delicious chili, chili your mamma's mamma is scared to try.",
      "userType" : "truck",
      "website" : ""
    }
  }

我想实现一个搜索栏,用于返回名称与搜索词匹配的卡车。例如,任何包含字符串“fries”的卡车名称都会将其 ID 提交给搜索结果 ID 数组。

这是我迄今为止尝试过的,但没有骰子。当我在搜索栏中输入薯条并点击搜索按钮时,它不会打印“6c542367-507c-4d01-af2c-bf93a7efaef4”

let usersRef = Firebase(url: "https://•••••••••.firebaseIO.com/users")

usersRef.queryOrderedByChild("name").queryEqualToValue(searchBar.text).observeEventType(.ChildAdded, withBlock:{
    snapshot in

    print(snapshot.key)
})

我什至不确定我有多接近,任何帮助将不胜感激。 谢谢!

【问题讨论】:

    标签: xcode swift firebase uisearchbardelegate


    【解决方案1】:

    您正在使用 queryEqualToValue (Query.equalTo()) 进行查询,只有在完全匹配的情况下才会返回结果。

    详细解释:

    如果我们在搜索栏中输入“fries”,它将查找“fries”的完全匹配,这在我们的文档中不可用,因此所需的值“6c542367-507c-4d01-af2c-bf93a7efaef4”是没有打印出来。

    如果我们在搜索栏中输入值“Pete's Fries”,那么我们将得到结果“6c542367-507c-4d01-af2c-bf93a7efaef4”

    如果我们为搜索提供部分值,那么我们正在尝试实现类似于 SQL 中的 LIKE Query 的搜索。请参阅以下帖子以获取有关“如何在 firebase 中执行 LIKE 查询”的更多信息

    How to perform sql "LIKE" operation on firebase?

    【讨论】:

    • 虽然这是对问题的一个很好的解释,但该链接依赖于 Elasticsearch 作为解决方案,我认为这不会在 iOS 设备上发生,因为它是一个 node.js 服务Elasticsearch on mobile devices .如果有人有更多信息,请发布!
    猜你喜欢
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    • 2021-12-07
    • 2012-09-14
    相关资源
    最近更新 更多