All right. 1
So our goal for this lesson is to be able to sort the messages that we retrieve from our database in 2
order of the time that it was sent. 3
The first thing that we need to figure out is how do we add a timestamp to the documents that we save 4
into our database so that when we create our documents. In addition to the messageSender and the messageBody, 5
we also want to add a key-value pair that is K.FStore.dateField, and the value here should 6
be the current time, 7
so the moment when this message was sent. And then once we have that value, then we'll be able to sort 8
it when we get our messages back from Firebase and we'll be able to order our messages chronologically. 9
So the way that we would get hold of the current time in Swift is through the use of a Date object. 10
So this is a specific point in time. 11
Let's initialize this Date object and we're going to tap into one of its methods which is called 12
timeIntervalSince1970. 13
This is something that programmers use a lot because what it gives us is the number of seconds since 14
January the 1st at zero hour in 1970. 15
And if you've taken the lesson on the Swift Deep Dive in loops, then you would have already seen me use 16
this in order to demonstrate loops. 17
So, now that we've got this extra field in our data, we have to go and delete all the current documents 18
in our collection because none of these actually have a date field. In order to delete a collection, 19
you just have to type in the ID which is messages, and then hit delete, and that empties out our collection. 20
And we can now go back over here and create new messages that have three properties: sender, body, and 21
date. 22
So let's go ahead and run this and create a new message, hit send, and maybe let's try a different message. 23
So we've now got two messages in here. And they should if we take a look inside our Firebase Firestore, 24
They should have a date field now attached to each of these documents and, indeed, they do. 25
So this is the number of seconds since 1970 and we can now use this, instead of the arbitrary ID to 26
sort our messages collection. 27
So if I go ahead and add some more messages, say, the most recent message, at some point the order breaks 28
down like what you saw previously, for example, this terribly auto-corrected Jill alays kajsd, 29
actually got inserted in a really random position, right? 30
So let's go ahead and sort the collection inside inside our loadMessages method. And the way that we do that 31
is, of course, by ordering our data. So we can call a method called order by and provide the key or the 32
field name. 33
So in this case, it would be that date field. And we can also, say, order by descending or ascending, and 34
we can even add other methods like limit the number of results that come back, plus lots of different 35
types of queries that Firebase provides. 36
Let's try and implement this order by method. 37
Notice how our DB dog collection sort of method call is getting lots of methods chained on to each other. 38
So I'm gonna go ahead and order the collection that I get back before I add my listener. 39
So right here, I'm going to write .order and I'm gonna order by a string which is gonna be the field 40
path, right? 41
And that is gonna be stored under K.FStore.date field. 42
So, now when I get my collection back, I'm going to order it by the datefield in ascending order, and 43
then I'm going to add my snapshotlistener to listen for changes. 44
Now, if this is getting pretty long for you, then an easy way of formatting this chained method is simply 45
by hitting enter on each of these dots. So you can see db.collection, then .order, 46
then .addSnapshotListener. 47
Just make sure you don't add anything here in between because, effectively, this is just shorthand for 48
them all to be continuous. But this way, it's easier to read on a screen that's not so wide. 49
So, now let's run our app again. And you can see our messages are now sorted in the order that they were 50
sent. 51
So if I were to send a message "A," and then "B," and then "C," you can see that they're being displayed in the 52
same order that they were created. 53
But when you take a look at our documents in here, for example, B, is way ahead of "A." And we're now ignoring 54
the ID of our documents, and instead, sorting by the date when they were created. The last thing to remember 55
before you move on is at the very beginning when we created our database under Cloud Firestore, 56
we set our rules so that basically anybody can read or write to it for 30 days. 57
So if you head over to the rules tab, you can see that we've still got the initial rule set which allows 58
anyone to read or write to our database. 59
So, now that we're done with testing Firebase, we're ready to update our rules, so that our database can 60
be more secure. So we click on that "Learn more" button, you can read about how Cloud Firestore Security 61
Rules work, or you can even watch a video on it. 62
Now, the version that we're going to go with is the rule that allows anybody who is logged in to be able 63
to read and write to our database, that includes all the users who have signed up to our app, so that 64
they can access the messages that they've sent before and to be able to send new messages to the database. 65
So we're gonna go ahead and copy this rule set from Auth required here, and we're going to select everything 66
that's currently in our rules and paste it in here, and then we're gonna go ahead and hit Publish. 67
So now we've secured our database. 68
It only allows read and write from people who are authenticated and we can now rest safely. 69
So, now we've seen how we can use Firebase Firestore to save data, to retrieve data, to listen for updates 70
on data, and also to sort and query our data. In the next lesson, 71
we're going to be spending some more time on improving our user experience. Because at the moment, 72
if you were to run this app on a physical device, there would be no way for you to send a message because 73
the keyboard that pops up completely obscures that button. 74
So we're going to be solving those problems and more on the next lesson. 75
So I hope I'll see you there.