查询Firebase中的特定字符串?

时间:2016-06-18 03:41:44

标签: xcode swift firebase uisearchbardelegate

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

  "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)
})

我甚至不确定我有多接近,任何帮助都会受到大力赞赏。 谢谢!

1 个答案:

答案 0 :(得分: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?