Swift - How to Use 'Shouldchangetextinrange' with Firebase for Real Time Searching

Swift - How Can I use 'shouldChangeTextInRange' with Firebase for real time searching?

This is not how Firebase works and you can't directly do a 'real time search' in Firebase for a substring. But, you can format your Firebase data to be searched and there are a couple of other methods which will also accomplish the goal.

One is straightforward: load your data from Firebase into an array and NSPredicate search (or other methods) through the array.

Another is that query'ing for full strings is easy; as long as the text is broken up into the chunks you want to search for:

people
person_0
first_name: "Ted"
last_name: "Stryker"
position: "sitting down and facing front"
person_1
first_name: "Teddy"

You can search for 'Ted' in first_name or 'Stryker' in last_name but you cannot search for 'front' in position.

Now the workaround and keep in mind disk space is cheap.

A node can be created that will let you search for stuff in a variety of ways

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

With this structure we can find all first names that start with Ted

    peopleRef.queryOrderedByChild("first_name/Ted").queryEqualToValue(true)
.observeEventType(.Value, withBlock: { snapshot in

for child in snapshot.children {
print(child.value)
}
})

Other searches can be performed; searching for all first names that contain e or Te etc.

If this is purely a situation where you are searching to perform an autofill in a text field (for example) pinging firebase on a keystroke by keystroke basis isn't going to work well.

Loading the data from Firebase into an array, or dictionary (or whatever) will probably be your best solution.

Firebase and indexing/search

Long-term, Firebase may have more advanced querying, so hopefully it'll support this sort of thing directly without you having to do anything special. Until then, you have a few options:

  1. Write server code to handle the searching. The easiest way would be to run some server code responsible for the indexing/searching, as you mentioned. Firebase has a Node.JS client, so that would be an easy way to interface the service into Firebase. All of the data transfer could still happen through Firebase, but you would write a Node.JS service that watches for client "search requests" at some designated location in Firebase and then "responds" by writing the result set back into Firebase, for the client to consume.
  2. Store the index in Firebase with clients automatically updating it. If you want to get really clever, you could try implementing a server-less scheme where clients automatically index their data as they write it... So the index for the full-text search would be stored in Firebase, and when a client writes a new item to the collection, it would be responsible for also updating the index appropriately. And to do a search, the client would directly consume the index to build the result set. This actually makes a lot of sense for simple cases where you want to index one field of a complex object stored in Firebase, but for full-text-search, this would probably be pretty gnarly. :-)
  3. Store the index in Firebase with server code updating it. You could try a hybrid approach where the index is stored in Firebase and is used directly by clients to do searches, but rather than have clients update the index, you'd have server code that updates the index whenever new items are added to the collection. This way, clients could still search for data when your server is down. They just might get stale results until your server catches up on the indexing.

Until Firebase has more advanced querying, #1 is probably your best bet if you're willing to run a little server code. :-)

Firebase .indexOn dynamic keys

For this type of query (and in general when you get into requiring adding indexes dynamically) you'll likely need to set up an inverted index. In your case:

{
"babies" : {
"-KQSVCXI-ZMz_lQ_Q906" : {
"name" : "Baby",
"parents" : {
"F0o8Gr5GC2SrakUYZpC20efcmk63" : true
}
},
"-KQSf4t9XQC3LnbsxLYS" : {
"name" : "Name2",
"parents" : {
"S6aO6Dx4lgg6anW9S9hu7EJrhVg1" : true
}
}
},
"parents": {
"F0o8Gr5GC2SrakUYZpC20efcmk63": {
"babies": {
"-KQSVCXI-ZMz_lQ_Q906": true
}
},
"S6aO6Dx4lgg6anW9S9hu7EJrhVg1": {
"babies": {
"-KQSf4t9XQC3LnbsxLYS": true
}
}
}
}

This type of bidirectional linking is fairly common in Firebase (and in many similar NoSQL databases).



Related Topics



Leave a reply



Submit