0

Suppose that I have a UISearchBar and I must perform a search of objects according to its names. But I want to perform this search by substrings in realtime. The problem is that there is not a method in the Firebase SDK for iOS to perform the search by substrings as "queryContainingSubstring:" or something.

In this post: Swift - How Can I use 'shouldChangeTextInRange' with Firebase for real time searching?, I have found two ways:

  1. Broke the name of the object and then querying if the object contains that substring, i.e:

    people {
      person_0 {
        first_name //First name is Ted from above
          T: true
          Te: true
          Ted: true
          ed: true
          d: true
          e: true
      }
      person_1 {
        first_name  //First name is Teddy from above
          T: true
          Te: true
          Ted: true
          etc
      }
    }
    
  2. Load all the data from Firebase into an array and NSPredicate search (or other methods) through the array.

The problem with the first way is that it can use a considerable space in the database when I have many objects. The problem with the second way is, for example, suppose that I have many data in the database, I'm pretty sure that we could have memory problems.

Is there any way to perform this search over the name of the object directly in the database? Have you any trick to perform this search?

Sorry for my English.

Community
  • 1
  • 1
  • 2
    The Firebase Database stores JSON. While you can build search tries in JSON to implement full-text search, it is not an ideal fit for this. That's why most developers integrate an external search engine, such as ElasticSearch or Algolia. See http://stackoverflow.com/questions/10559191/firebase-and-indexing-search – Frank van Puffelen Jun 28 '16 at 15:38
  • very informative @frank, thanks – Fattie Jun 28 '16 at 15:44
  • 1
    Typically Firebase isn't a good fit for direct auto-fill use cases due to the lagginess of the internet. You are much better off loading the possible strings from Firebase into an array and then filter the array using array.localizedStandardContainsString. The post you mention is also an alternative if you just need a substring search but not realtime. Oh, and space wise, those strings are pretty tiny so you could store thousands of them without an issue. – Jay Jun 28 '16 at 17:22
  • Thanks guys. I'm testing Algolia, thanks @Frank. – Mario Villamizar Palacio Jun 28 '16 at 19:47

0 Answers0