How to display data from Firebase faster?
Data is loaded from Firebase asynchronously, since it may take some time to come down from the server/network. Your main application code continues to run while the data is being loaded. Then once the data is available, your completion handler is called.
For this reason, any code that requires data from the database, must be inside the completion handler, or called from there. Putting it anywhere else makes it uncertain that the data will have loaded by the time the code needs it.
So for example:
let uid = Auth.auth().currentUser?.uid
let ref = Database.database().reference()
ref.child("users").child(uid!).child("weight").observe(.value) { (snapshot) in
self.userWeight = snapshot.value as! Float
print(self.userWeight)
self.maxAmountOfWater = (self.userWeight * 4) / 100
maxWaterLabel.text = String(maxAmountOfWater)
}
Not the use of self.userWeight
is inside the callback, so it has access to the value from the database as soon as it's loaded.
If you want you can also define your own completion handler, that you then call from within the Firebase completion handler, or use a dispatch group. For some examples of this, see:
- Finish all asynchronous requests before loading data?
- Returning method object from inside block
- Is this a good way to display asynchronous data?
Edit:
Just a note that UI updates within a Firebase Closure are called on the main thread. So for example, if you load a tableView dataSource within the closure, you can call tableView.reloadData() within the closure as well without having to use a dispatch group or other thread.
how to make getting data faster from Firebase Firestore?
That performance sounds a bit worse than what I see, but nothing excessive. Loading data from the cloud simply takes time. A quick approach to hide that latency is by making use of Firebase's built-in caching.
When you call getDocuments
, the Firebase client needs to check on the server what the document's value is before it can call your code, which then shows the value to the user. As said: there is no way to speed up this reading in your code, so it'll always take at least 1.8s before the user sees a document.
If instead you listen for realtime updates from the database with addSnapshotListener
, the Firebase client may be able to immediately call your code with values from its local cache, and then later re-invoke your code in case there has been an update to the data on the server.
How to improve a performance of the query to Firebase firestore
There's nothing you can change about the code you shared to improve the performance. Your only options would be to get a faster internet connection (lower latency and/or more bandwidth).
From the screenshot it seems to take 53ms to read the document, which seems pretty reasonably to me at first glance.
How to (quickly) download huge amounts of data in Firebase?
If the timeout is caused by the fact that you're trying to read/return too much data in one go, you might want to consider limiting how much data you return. By adding a limit()
clause to your query, you can limit how much data it returns at most.
const nD1 = db.collection('grind-bit').doc(req.query.serial).collection('history')
.where('date', '>=', startdate).where('date', '<=', enddate)
.limit(100)
.get().then(snapshot => {
snapshot.forEach(doc => {
...
This of course means that you may have to call the HTTP function multiple times to ensure you process all data. The simplest approach for that is: if you get 100 results (or whatever limit your specify), try another time after processing them.
Also see:
- The Firebase documentation on limiting query results
Related Topics
Module Compiled With Swift 3.0 Cannot Be Imported in Swift 3.0.1
How to Document the Parameters of a Function'S Closure Parameter in Swift 3
What's the Difference Between Using Aranchor to Insert a Node and Directly Insert a Node
What Does an Ampersand (&) Mean in the Swift Language
Why Are Doubles Printed Differently in Dictionaries
Convert Swift Array to Dictionary With Indexes
How to Convert Hex Number to Bin in Swift
How to Save a Struct to Realm in Swift
Is Force Cast Really Bad and Should Always Avoid It
Safariviewcontroller: How to Grab Oauth Token from Url
Do Capture Lists of Inner Closures Need to Redeclare 'Self' as 'Weak' or 'Unowned'
Uitextview Highlight All Matches Using Swift
When to Use Foreach(_:) Instead of for In
Where Is Info.Plist in Xcode 13? (Missing, Not Inside Project Navigator)