0

I'd like to make a search bar, and if I get the search word, I'd like to get the data which include the word on firebase.

I've already implimented a search bar and am looking for what I have to write in func searchBarSearchButtonClicked(_ searchBar: UISearchBar).

So far, I have an idea. It uses "for" and "if" to run code like this to get data.

for i in (~) {
    let defaultPlace = DBRef.child("posts/\(i)/age")
    defaultPlace.observe(.value) { (snap: FIRDataSnapshot) in 
        if ~ {
            self.displayAge.text = (snap.value! as AnyObject).description
        }
    }
}

(↑ this is just an example. "~" is something)

But I'm not sure if it's really the best way.

halfer
  • 19,824
  • 17
  • 99
  • 186
Yutaro
  • 89
  • 1
  • 10
  • https://stackoverflow.com/questions/38618953/how-to-do-a-simple-search-in-string-in-firebase-database Although it is for android you could apply it for iOS. – Ziya ERKOC Aug 15 '17 at 11:05

1 Answers1

0

iOS:

queryOrdered(byChild: "{field name}").queryStarting(atValue: "{value}")

replace {field name} and {value}

Codus
  • 1,433
  • 1
  • 14
  • 18
  • Thank you for your answer! This is maybe an elementary question, but what is {field name}? And should I input a search word to {value}? – Yutaro Aug 15 '17 at 11:17
  • `field name` is the field you want to search for. `value` is text of user input in search bar – Codus Aug 15 '17 at 11:20
  • Thank you! But if I write let result = DBRef.queryOrdered(byChild: "title").queryStarting(atValue: searchtext) print(result), it returns only (/ { i = tite; sp = "\U9752"; }). Is this wrong? – Yutaro Aug 16 '17 at 05:14