Check out a free preview of the full Firebase Fundamentals course
The "Firestore Realtime Stream Solution" Lesson is part of the full, Firebase Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:
David walks through the solution to the Firestore realtime stream exercise.
Transcript from the "Firestore Realtime Stream Solution" Lesson
[00:00:00]
>> So how was that exercise? It's good, we got stuff going to the screen? All right, I'm gonna take you through what I do in this case. So I have my document snapshot being passed in every time it's being mapped over. So I can do something like a D structure and call docsnapshots.data.
[00:00:21]
And what are we getting out of that? I need to get the uid, the typo of Fist which should be first. So I could say uid, first, last, so first, last, highscore, city, highscore and city. And from here I want to also get, actually do I want the ID?
[00:00:42]
No, I have everything I need except for fromCache. So if I wanna see if it's from the cache, I can say docSnapshot.metadata. And this right here will tell me if it was from cache. Let's see here. I usually use TypeScript, so I get really scared when things don't give me types, but this is just in the JavaScript world.
[00:01:09]
So from here, I can return uid, last, first, highscore, city, fromCache. So now when I save, Refresh, look at that. Now, this all says false from cache and no matter what I do, it says true now, I think it's because I, let me make sure so I don't.
[00:01:40]
Did I do this, yes, I enabled persistence. I didn't need to have it enabled for this demo, so let me comment that out. No matter what I do, when I refresh, it's gonna say false. And it's because I don't have any offline persistence being enabled. And just as we saw here, I have this enable offline section, which you can take a look into, which is just sort of like our initialized app just with better structure to it.
[00:02:15]
And I call enable multitab persistence, pass in firestore. And from there, I have offline tab persistence. And then now when I refresh, true. So it's only syncing changes that happen behind the scenes from the server. It's not doing new reads every single time, it's only getting the updates.
[00:02:37]
And so the cache is really good for knowing that. And if I even did has pending rights, we could see from there. Let's see if I wanted to just do console.log, let's just do all of docsnapshot.metadata. Save, let's open this up. And we can see that all across the board has pending writes is false because we're not doing anything.
[00:03:11]
So that is how we we read data from a real-time stream. But we also can take other bits of the document snapshot to surface more of a functional and usable object for our interface to be able to do things on. And so this is sort of the basis of what we're gonna be doing throughout the next sections.
[00:03:29]
Because all we've done right now as we've said, get me data at a collection. And what we can see though is that this user's collection, it is big. I scroll through, I think it's 250 users. And this expenses, I believe it's 2,500 maybe, depending on how I loaded in, 5,000 expenses.
[00:03:50]
Probably don't wanna be reading all these back to the client in real time. So what we're gonna be doing when we get back is we're gonna be talking about querying in Firestore. And we're gonna take a look at things like simple queries, or just doing simple limits and simple ways.
[00:04:04]
And we're even gonna be doing more complex things. And we're gonna be doing a lot of comparisons to how that works in the SQL world versus the NoSQL world.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops