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

  "trucks" : {
    "3705ec54-8a2e-4eb1-8bb9-ab2243645ac1" : {
      "email" : "[email protected]",
      "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" : "[email protected]",
      "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" : "[email protected]",
      "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" : "[email protected]",
      "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)
})


我什至不知道我有多近,任何帮助将不胜感激。
谢谢!

最佳答案

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

详细说明:

如果在搜索栏中输入“炸薯条”,它将查找“炸薯条”的完全匹配项,该匹配项在我们的文档中不可用,因此未打印所需的值“ 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?

10-08 18:44