1

I have a dating application iOS and Android that's retrieving keys slow up-to 10 seconds. I've followed all the recommendations on firebase real-time documentation but as I'm getting more users it's affecting the performance of my application. I've included ".indexOn": ["g"] and doing my filtering once I receive the keys by putting the information I need onto the key so I don't have to do a second query since geofire doesn't support querying. While geofire observing keys the app is practically unusable until it completes its read. I'm very fond of firebase but should I be considering migrating to another service for scalability and efficiency?

DispatchQueue.global(qos: .background).async {
    let result = userDistance * 1609.344
    let center = CLLocation(latitude: userlat, longitude: userlon)
    let searchRegion = MKCoordinateRegion(center: center.coordinate, latitudinalMeters: result, longitudinalMeters: result)
    let geofireRef = Database.database().reference().child("location")
    let geoFire = GeoFire(firebaseRef: geofireRef)
    print("\(startingPoint.timeIntervalSinceNow * -1) Started Here")

    geoFire.query(with: searchRegion).observe(.keyEntered) {  (key: String!, location: CLLocation!) in
        print("key entered")
    }
    geoFire.query(with: searchRegion).observeReady {
        print("\(startingPoint.timeIntervalSinceNow * -1) Finished Here")

    }
}

Structure

--> location
---> key
----> g: <geohash>
-----> l: 
-------> 0: Lat
-------> 1: Lon

Index:

  "location": {
    ".read": "auth !== null",
        ".write": "auth !== null",
    ".indexOn": ["g"]
    }

Query Test:

ref.child("location").queryOrdered(byChild: "g").queryLimited(toFirst: 20000).observeSingleEvent(of: .value) { (snapshot) in
    print("\(timeit.timeIntervalSinceNow * -1) Finished Here")
}
EmanRobi
  • 21
  • 5
  • Sorry to hear about the performance problems with GeoFire. Instead of describing your code, please edit your question to show the [exact steps, code, data, and rules with which any of us can troubleshoot and reproduce the issue](http://stackoverflow.com/help/mcve). I highly recommend studying that link, as fllowing the guidance in there is the best way to increase the chances that someone can help. – Frank van Puffelen Feb 19 '21 at 16:04
  • Hi @FrankvanPuffelen! I have your videos on repeat trying to improve the speed . Do you have any suggestions? Performance goes significantly down when a user is retrieving keys in an area that has thousands of people. I've also edited my question so it includes how I pull the data as well as the structure. The time intervals are just for me to see how long it takes to get the keys. – EmanRobi Feb 19 '21 at 23:00
  • How many keys are under `location`? It shouldn't matter too much (as long as you have an index on `g`), but it might help us find the cause of the problem. (Code that perform the queries on RTDB: https://github.com/firebase/geofire-objc/blob/fe13c8d30a6a51dd5e22a109648572bf53414298/GeoFire/Implementation/GFQuery.m#L210-L214) – Frank van Puffelen Feb 19 '21 at 23:21
  • Hi @FrankvanPuffelen, there are approximately 10k keys. The more users that are added geofire query takes longer to retrieve the keys? Unfortunately, I'm unable to find anything that would allow me to limit the number of results which would probably speed things up? – EmanRobi Feb 20 '21 at 01:39
  • Hmmm... 10k keys should not hit any limits/bottlenecks on the server, which makes me suspect that all data is being downloaded to the client and filtered there. Can you: 1) show how you define the index in your rules? 2) show the exact JSON instead of the schematic representation (as text, no screenshots please). You can get this by clicking the "Export JSON" link in the overflow menu (⠇) on your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Feb 20 '21 at 02:34
  • Hi @FrankvanPuffelen I've included the rule for location and some data from the JSON. I did some testing by adding 20k more thousand entries of dummy data and it took 35 seconds to receive 28k keys. Is that unusual? – EmanRobi Feb 20 '21 at 02:49
  • That all sounds very unusual, and your JSON/index look good. Can you try running a regular Firebase query, as the one shown in the code I linked above on a small range of the keys? I wanna know if the performance problem also occurs when you only use RTDB, or whether it happens only in GeoFire. – Frank van Puffelen Feb 20 '21 at 02:56
  • Hi @FrankvanPuffelen, I'm not familiar with running obj C but I did a simple query using swift ordering by "g". If I limit it to about 100 keys it undertakes 1 second to complete and it takes 7 seconds to go over 20k keys. I've included the query I used above at the end of the question. – EmanRobi Feb 20 '21 at 03:38
  • OK, thanks for testing that. It really sounds like it's downloading all data to the iOS app, and doing the filtering there. I just don't understand why that happens, since you have he right index. Can you check the debug output of the app, for a warning about it downloading all data? As you might notice, I'm clutching at straws here, so I hope somebody else spots what's going on. – Frank van Puffelen Feb 20 '21 at 04:32
  • @FrankvanPuffelen, I'm not seeing anything in the debug tool suggesting that it's getting all the data but it sure seems like it. I would imagine that it should take 2-3 seconds to pull all the necessary keys. Any idea how long it should take to fetch 20k keys? – EmanRobi Feb 20 '21 at 05:00
  • If the query happens on the server, it shouldn't take much time at al. If it happens on the client, the amount of time is dependent on the size of the JSON under `location` and the bandwidth of the device. But again: this query is supposed to happen on the server, so I have no idea what is going wrong here. – Frank van Puffelen Feb 20 '21 at 15:27
  • @FrankvanPuffelen do you think you could connect me with someone who could help debug and fix my issue? – EmanRobi Feb 20 '21 at 22:06
  • Oddly enough, geofire is working just fine on my android app but on my iOS version, it is having large delays. – EmanRobi Feb 21 '21 at 10:31
  • Hi @FrankvanPuffelen, I was wondering if you had a workaround for this bug? – EmanRobi Apr 23 '21 at 23:31
  • I'm not assuming this is a bug. But to move forward, you might want to start by reducing the number of keys you retrieve. How long does it take with 100 keys? With 200? With 1000? Etc. How much data is being read in each of those cases? How large is the data that is retrieved? How is the bandwidth of the device? If you raise/lower bandwidth, does that linearly also change the time it takes? – Frank van Puffelen Apr 23 '21 at 23:42

0 Answers0