Reputation: 21
When observing a FIRDatabaseQuery with the observe method, it sometimes takes 10~13 seconds to get the data from the remote database. This mostly happens when the app is coming from the background or when it has just started up. After this query has completed, subsequent queries are returning super fast with remote data. And the problem doesn't occur anymore.
This is the code I am using to observe:
public static func observeMatchEvents(match: Match) -> QueryObserver
{
FIRDatabase.database().reference().child("events").queryOrdered(byChild: "match_id").queryEqual(toValue: match.identifier)
log.debug("Started getting match events...")
let observer = query.observe(FIRDataEventType.value, with: { (snapshot) in
log.debug("This sometimes takes 10~13 seconds before it gets executed")
// Handle data
})
return QueryObserver(query: query, handle: observer)
}
This is the structure I have in my Firebase Database:
Where events is a child of the root of the database.
I am using Firebase Rules to improve the indexing as described in the documentation:
"events": {
".indexOn": ["match_id", "type", "sub_type"]
}
I am thinking this has something to do with sockets having to be opened upon startup. However 10~13 seconds takes really long. Is anyone else experiencing this behaviour and does someone have an idea to speed this up?
EDIT: So after trying the suggestions posted here and trying other things I finally found a solution. There were actually two problems:
1) The slowdown occured when I started observing too queries at the same time. As the amount of data was pretty huge it took a subsequent long time to complete those observe calls. This was also visible in the timeprofiler where you would see that the CPU load at this time was pretty huge.
2) Something that I found out that is more troubling is that was setting FIRDatabase.database().persistenceEnabled = true dramatically increased callback time from queries. When doing a clean install of the app and getting data from Firebase it was really fast. However the response times would increase everytime the Firebase Database would write more data to disk, this was increasing over 10 ms per request. This way the app would eventually take up to 10 seconds to complete a request to the database. We noticed this same behaviour for our Android app aswell. The only solution here was to disabled persistence on the database entirely.
Upvotes: 2
Views: 2936
Reputation: 598728
Since you say this slowdown only occurs on the initial query when the app becomes active, it seems most likely that it's caused by the Firebase client connecting to its server.
There is little your app code, the Firebase client or the Firebase servers can do about this. But you can get some insight into what is happening by enabling debug logging and studying the output when it makes the initial connection.
Upvotes: 1
Reputation: 28750
Doing searches depending on the size of your database will be slow. Ideally you'd create a different node in your database where the key is the match_id and then it would be fast to search. Something like this:
{
"matches": {
"vodiudtnlmdrjiqhkkdowc15": {
"subvodiudtnlmdrjiqhkkdowc156hii1jc7phspraa81xp9kuj85ezviixcyaaj162saxs2yl127s9": true
}
}
}
Then you can query for:
.child('matches').child(match.identifier)
This will get you all events that have that match id. Once you have that you can loop through those and get the individual records directly. In order to keep this data up to date look into firebase functions or roll your own.
This will be extremely fast as it will not need to do a search at all.
Upvotes: 2