Either everyone will come back.
Johnny B here with the slopes dot com.
And this lesson we're going to get into some real good firestorm stuff.
All right.
So let's get started.
And the last lessons we have laid the groundwork to do some really cool stuff are I we have our UI all
set up.
We have this jump into our database here.
We have a collection with documents in it.
And so no we want to be able to get these documents into our app.
Right.
So let's figure out how to do that together.
So let's take a step back and think about what we need to do when we add documents to our collection.
We get a path to the collection.
So let's jump into our ADD thought ABC and revisit it.
We say fire firestorm off I was Torridon collection thoughts ref and then we add documents to it.
Well the way to retrieve documents is actually pretty similar.
It's just pretty much the this in reverse.
Instead of adding data to a document we are going to be retrieving those documents and then parsing
out the data that comes from it.
All right so here in Maine Visi does swift instead of doing what we do here where we have this this
is fine here because we only call this once.
But in the mean receiver We'll be calling it several times.
So it's going create a variable for it and we're going to call that yes a private var and this is going
to be the thoughts collection ref for reference.
And that's going to be of type of collection.
See here it's not the auto completing because we do not have firebase imported.
So if you're not getting the auto completion that you expect it's often because you haven't imported
that copal yet.
All right so the collection reference there we go.
And here we can see that it is a object can be used for adding documents getting document references
and querying for documents using the methods inherited from fire query.
All right so that is what we want a collection reference.
And then here in view to load we will initialize it.
So when I say thoughts collection ref is equal to fire.
Store that fire store that collection path and the path is thoughts.
RATH So now we have a reference to our thoughts collection.
OK.
So this is the first collection ref references this collection here in our database.
All right so we're going to start out simple here in let's let's get our view did up here OK or let's
do of you will appear.
You will appear here.
Let's go ahead and make our first fetch call.
OK.
So what we do is we can say thoughts collection ref stopped.
And instead of adding documents we are going to get documents OK.
And so here we have a completion handler as well so I'm going to say return and then press return again.
And in our completion we get two parameters.
We get the snapshot and an error and optional error.
Both of them are optional.
All right so here we're going to call snap shot and error.
OK.
And so what the snapshot is is this a snapshot of what the what your data looks like at the point that
it is fetched.
OK and so let's go ahead and here we'll just say let air is equal to error and we'll just go ahead and
debug printed with say Debug.
Print and will say error fetching docs and will print that out.
So here is error there we go.
Else we have the snapshot.
And for now let's just go ahead and try printing that out.
I want to say print.
And when I say snap shot dot documents and let's see let's see what we get.
So let's see here it looks like we are printing out fire document snapshots.
OK so each of these represents one of our documents in our collection we have four.
You can see here and here in our database you can see that we also have four here.
So let's get a little bit more specific.
And what is this error.
Oh this should be from the air VRR.
Let's try saying for document in snapshot
documents.
OK.
So we're going to loop through all the documents in the snapshot and then it's going to ask us to unwrap
this which is fine for now since we're just trying to trying to take a look at the data.
And so say a print document that data.
OK.
So what we're doing here is we're saying.
So we're saying snapshot documents.
OK.
And so the snapshot is what this looks like at the time that we fetch it.
And then the documents and our documents right here.
This is each of these right here.
And then we're going to print the data that is associated with each document.
And so we should see printed out a dictionary containing all of this info for each document.
All right when we run this when I run it.
All right.
And so now for you to take a look here we can see that we are indeed printing out the dictionary for
each of our documents.
All right.
So we have all of the information here.
All right so you're starting to see how the data is going to be structured.
When we get it from fire store.
All right.
So we have a snapshot which contains all of the documents or whatever it is that we are fetching.
It could just be a single document that we are fetching in which case that snapshot would be the single
document.
But in this case when we are when our reference is a collection then we're getting back all of the documents.
All right so what do we need to do.
We need a way to parse out this information from the document and put it into a thought class and then
then we can display it.
Right.
So let's go ahead and do that instead of having this force and we're up here let's go ahead and do it.
Card statements when I say guard that snap equal snap shot else return OK and then we'll save for document
in right here will say Snap dot documents.
There we go.
And then here we're going to parse out that information.
It was very similar to Jaison parsing what we have is a dictionary of key string and value any.
All right so we're going to need to supply the key that we are looking for and then cast it to the type
that we want it to be so we can say let data equal document that data and then we can just go ahead
and extract out of that data the information that we want.
All right.
So we need the username user name equal data and then going into it and extracting it from the key that
is username and then remember this is going to be of type.
Any word on when we get it.
We have to cast it to the type that we want.
And then I'm going to go ahead and use the no coalescing to supply a default value if there isn't one.
And when I say anonymous if there is no value then I'm going to go to the next one which is timestamps
and say Let time stamp equal data and it's key is also time stamp.
And that is a date.
OK.
And if there isn't a date then we'll go ahead and just initialize it to be the date of now which is
fine.
All right and then let's see if we can save some time by copying and pasting the user name.
Since most of them are strings in case so let the next one is a thought text.
And its key is thought to text and it is a string.
If there isn't anything we're just going to do an empty string and then next we're going to say let
some likes equal to the data and the key is numb likes and you know what I just realized I forgot that
we have constants for all of these.
So this should actually be user name.
This should be a time stamp.
This should be thought text and this should be numb likes.
There you go.
And this should be cast as it is.
No.
Then we'll set it to default zero when we need it.
No comments.
People turn.
And it's key is numb underscore comments and that we shall set our cast as an int and if there isn't
one will say that as a default value to zero.
And then lastly we need the document ID document ID equal to and this is going to be a document that
document ID.
OK.
So to get the document ID all we have to do is grab the document that is inside of snapshot documents
and get the document ID property from it.
Pretty easy.
All right so now let's go ahead and initialize a thought from our thought class and then what we're
going to do is we are going to append it to our thoughts right here.
And then guess what we're just going to reload our table view.
And we should see all of these thoughts being displayed in our table view.
All right so we're actually pretty close here.
So I'm going to say let new thought equal and construct a new thought from our class.
Initialiser And here we go.
So user name is equal to user name.
Timestamp is equal to time stamp thought text is equal to last year thought text to thought text number
of likes is numb likes comments is Gnome comments and document ID is document ID.
All right so now we have this new thought we're going to say thoughts that are penned and the new element
that we are spending is this new thought.
Already And now that that is done what we're going to do is going to say table you dot reload data you're
going to see this.
And if it's here we need our selfs on here since we inside of a closure.
And if I didn't forget anything which is improbable.
But we'll see.
Then we should be able to see these thoughts being displayed in our table views so let's run it and
see you know this isn't it.
Let's see here.
These are all of our static table view cells that see oh you know what we forgot in our thought.
So we forgot to actually do the configuring of the cell so we did everything else but we actually forgot
the final step which is to configure the cell.
All right so let's do it.
We pass into this function a cell.
So it's a user name label that text is equal to the thought that we pass into it and we access the username.
There we go.
Then time stamp label time stamp label that text is equal to fat dot the time stamp.
Then a thought text label that text as you do thought dot dot text and likes no label that text is equal
to.
We have to do some string interpolation here let's say.
Or do we need to let's say strings or string and thought that name likes.
There we go.
And here we're trying to say can I assign a value of type data type string.
For now let's actually just leave this alone.
So I'm going to save this and run it and see OK.
There we go.
Now we're actually seeing the data from our database that is actually pretty exciting.
And man it looks pretty good doesn't it.
All right well done.
So let's just recap the flow of how this is all working.
So here in our main VCM view will appear we are getting a reference to our collection then we're saying
get documents.
And so we are fetching.
This is a one time fetch we're fetching all of the documents in a snapshot.
OK.
Then we we we do an unwrapped on the snapshot calling it snap and then we look through all of the documents
inside of that snapshot.
And so each document has data attached with it which is everything that we have here in our database
and it is returned to us in a dictionary.
And so all we have to do then is extract that information that we need from the dictionary construct
a new thought from our thought class append that thought to our thoughts array right here and then we
reload the table view data.
This calls our table view methods which then loops through and using our thoughts array as the data
source.
We go ahead and configure the cells pass into it.
The thought that corresponds to that cell in the in the index path rope.
And here in that cell we configure the cell set to the IB outlets for each individual cell based on
the thought that is passed into it and the end result is what you see here.
All right.
So that's that's pretty cool.
All right so I think we're going to end this lesson right here in the next lesson.
We're going to go over how to set up listeners so that we can see changes in real time as well as sorting
and filtering.
All right.
So up here except for that and I'll see you in the next one.