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