1
00:00:05,760 --> 00:00:07,230
Either everyone will come back.

2
00:00:07,240 --> 00:00:09,410
Johnny B here with the slopes dot com.

3
00:00:09,550 --> 00:00:12,630
And this lesson we're going to get into some real good firestorm stuff.

4
00:00:12,630 --> 00:00:12,930
All right.

5
00:00:12,970 --> 00:00:14,220
So let's get started.

6
00:00:14,500 --> 00:00:20,170
And the last lessons we have laid the groundwork to do some really cool stuff are I we have our UI all

7
00:00:20,170 --> 00:00:20,920
set up.

8
00:00:20,920 --> 00:00:24,210
We have this jump into our database here.

9
00:00:24,280 --> 00:00:27,420
We have a collection with documents in it.

10
00:00:27,700 --> 00:00:32,140
And so no we want to be able to get these documents into our app.

11
00:00:32,140 --> 00:00:32,690
Right.

12
00:00:32,860 --> 00:00:35,500
So let's figure out how to do that together.

13
00:00:35,500 --> 00:00:41,980
So let's take a step back and think about what we need to do when we add documents to our collection.

14
00:00:41,980 --> 00:00:44,570
We get a path to the collection.

15
00:00:44,560 --> 00:00:47,960
So let's jump into our ADD thought ABC and revisit it.

16
00:00:48,250 --> 00:00:54,970
We say fire firestorm off I was Torridon collection thoughts ref and then we add documents to it.

17
00:00:55,300 --> 00:00:59,500
Well the way to retrieve documents is actually pretty similar.

18
00:00:59,800 --> 00:01:02,070
It's just pretty much the this in reverse.

19
00:01:02,080 --> 00:01:08,500
Instead of adding data to a document we are going to be retrieving those documents and then parsing

20
00:01:08,500 --> 00:01:10,380
out the data that comes from it.

21
00:01:10,600 --> 00:01:18,020
All right so here in Maine Visi does swift instead of doing what we do here where we have this this

22
00:01:18,110 --> 00:01:20,560
is fine here because we only call this once.

23
00:01:20,740 --> 00:01:24,610
But in the mean receiver We'll be calling it several times.

24
00:01:24,610 --> 00:01:31,060
So it's going create a variable for it and we're going to call that yes a private var and this is going

25
00:01:31,060 --> 00:01:37,110
to be the thoughts collection ref for reference.

26
00:01:37,120 --> 00:01:40,850
And that's going to be of type of collection.

27
00:01:41,320 --> 00:01:47,890
See here it's not the auto completing because we do not have firebase imported.

28
00:01:47,990 --> 00:01:52,610
So if you're not getting the auto completion that you expect it's often because you haven't imported

29
00:01:52,700 --> 00:01:53,880
that copal yet.

30
00:01:54,200 --> 00:01:57,240
All right so the collection reference there we go.

31
00:01:57,470 --> 00:02:02,810
And here we can see that it is a object can be used for adding documents getting document references

32
00:02:02,810 --> 00:02:06,710
and querying for documents using the methods inherited from fire query.

33
00:02:06,710 --> 00:02:09,750
All right so that is what we want a collection reference.

34
00:02:10,010 --> 00:02:12,440
And then here in view to load we will initialize it.

35
00:02:12,520 --> 00:02:17,430
So when I say thoughts collection ref is equal to fire.

36
00:02:17,450 --> 00:02:27,050
Store that fire store that collection path and the path is thoughts.

37
00:02:27,070 --> 00:02:32,280
RATH So now we have a reference to our thoughts collection.

38
00:02:32,290 --> 00:02:32,680
OK.

39
00:02:32,870 --> 00:02:38,720
So this is the first collection ref references this collection here in our database.

40
00:02:38,720 --> 00:02:45,470
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

41
00:02:45,470 --> 00:02:46,700
do of you will appear.

42
00:02:46,930 --> 00:02:49,290
You will appear here.

43
00:02:49,340 --> 00:02:51,840
Let's go ahead and make our first fetch call.

44
00:02:51,860 --> 00:02:52,330
OK.

45
00:02:52,580 --> 00:02:57,060
So what we do is we can say thoughts collection ref stopped.

46
00:02:57,350 --> 00:03:01,920
And instead of adding documents we are going to get documents OK.

47
00:03:02,180 --> 00:03:08,630
And so here we have a completion handler as well so I'm going to say return and then press return again.

48
00:03:08,630 --> 00:03:12,380
And in our completion we get two parameters.

49
00:03:12,380 --> 00:03:15,930
We get the snapshot and an error and optional error.

50
00:03:15,980 --> 00:03:17,330
Both of them are optional.

51
00:03:17,330 --> 00:03:22,640
All right so here we're going to call snap shot and error.

52
00:03:23,200 --> 00:03:23,910
OK.

53
00:03:23,940 --> 00:03:30,350
And so what the snapshot is is this a snapshot of what the what your data looks like at the point that

54
00:03:30,350 --> 00:03:31,750
it is fetched.

55
00:03:32,210 --> 00:03:40,340
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

56
00:03:40,340 --> 00:03:43,130
debug printed with say Debug.

57
00:03:43,220 --> 00:03:54,600
Print and will say error fetching docs and will print that out.

58
00:03:54,650 --> 00:03:57,670
So here is error there we go.

59
00:03:57,680 --> 00:04:01,360
Else we have the snapshot.

60
00:04:01,460 --> 00:04:04,610
And for now let's just go ahead and try printing that out.

61
00:04:04,610 --> 00:04:05,750
I want to say print.

62
00:04:06,170 --> 00:04:15,600
And when I say snap shot dot documents and let's see let's see what we get.

63
00:04:15,650 --> 00:04:22,980
So let's see here it looks like we are printing out fire document snapshots.

64
00:04:23,150 --> 00:04:27,860
OK so each of these represents one of our documents in our collection we have four.

65
00:04:27,870 --> 00:04:33,560
You can see here and here in our database you can see that we also have four here.

66
00:04:33,830 --> 00:04:35,610
So let's get a little bit more specific.

67
00:04:35,630 --> 00:04:36,680
And what is this error.

68
00:04:36,700 --> 00:04:40,860
Oh this should be from the air VRR.

69
00:04:41,420 --> 00:04:48,790
Let's try saying for document in snapshot

70
00:04:52,770 --> 00:04:54,010
documents.

71
00:04:54,510 --> 00:04:55,000
OK.

72
00:04:55,020 --> 00:05:02,280
So we're going to loop through all the documents in the snapshot and then it's going to ask us to unwrap

73
00:05:02,280 --> 00:05:06,930
this which is fine for now since we're just trying to trying to take a look at the data.

74
00:05:07,080 --> 00:05:13,660
And so say a print document that data.

75
00:05:13,950 --> 00:05:14,400
OK.

76
00:05:14,580 --> 00:05:17,000
So what we're doing here is we're saying.

77
00:05:17,280 --> 00:05:19,480
So we're saying snapshot documents.

78
00:05:19,530 --> 00:05:19,810
OK.

79
00:05:19,810 --> 00:05:25,030
And so the snapshot is what this looks like at the time that we fetch it.

80
00:05:25,140 --> 00:05:28,590
And then the documents and our documents right here.

81
00:05:28,590 --> 00:05:31,480
This is each of these right here.

82
00:05:31,740 --> 00:05:37,130
And then we're going to print the data that is associated with each document.

83
00:05:37,290 --> 00:05:43,820
And so we should see printed out a dictionary containing all of this info for each document.

84
00:05:43,830 --> 00:05:47,480
All right when we run this when I run it.

85
00:05:47,500 --> 00:05:48,500
All right.

86
00:05:48,630 --> 00:05:55,800
And so now for you to take a look here we can see that we are indeed printing out the dictionary for

87
00:05:55,860 --> 00:05:57,610
each of our documents.

88
00:05:57,610 --> 00:05:57,890
All right.

89
00:05:57,900 --> 00:06:00,230
So we have all of the information here.

90
00:06:00,420 --> 00:06:04,640
All right so you're starting to see how the data is going to be structured.

91
00:06:04,740 --> 00:06:07,100
When we get it from fire store.

92
00:06:07,110 --> 00:06:07,370
All right.

93
00:06:07,380 --> 00:06:13,980
So we have a snapshot which contains all of the documents or whatever it is that we are fetching.

94
00:06:13,980 --> 00:06:18,570
It could just be a single document that we are fetching in which case that snapshot would be the single

95
00:06:18,570 --> 00:06:19,380
document.

96
00:06:19,440 --> 00:06:26,040
But in this case when we are when our reference is a collection then we're getting back all of the documents.

97
00:06:26,340 --> 00:06:27,430
All right so what do we need to do.

98
00:06:27,480 --> 00:06:36,470
We need a way to parse out this information from the document and put it into a thought class and then

99
00:06:37,390 --> 00:06:38,250
then we can display it.

100
00:06:38,250 --> 00:06:38,840
Right.

101
00:06:39,090 --> 00:06:42,990
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.

102
00:06:42,990 --> 00:06:57,350
Card statements when I say guard that snap equal snap shot else return OK and then we'll save for document

103
00:06:57,410 --> 00:07:05,450
in right here will say Snap dot documents.

104
00:07:05,460 --> 00:07:06,480
There we go.

105
00:07:06,510 --> 00:07:09,040
And then here we're going to parse out that information.

106
00:07:09,210 --> 00:07:18,060
It was very similar to Jaison parsing what we have is a dictionary of key string and value any.

107
00:07:18,060 --> 00:07:25,170
All right so we're going to need to supply the key that we are looking for and then cast it to the type

108
00:07:25,170 --> 00:07:35,790
that we want it to be so we can say let data equal document that data and then we can just go ahead

109
00:07:35,820 --> 00:07:39,000
and extract out of that data the information that we want.

110
00:07:39,000 --> 00:07:39,250
All right.

111
00:07:39,270 --> 00:07:48,420
So we need the username user name equal data and then going into it and extracting it from the key that

112
00:07:48,420 --> 00:07:52,430
is username and then remember this is going to be of type.

113
00:07:52,470 --> 00:07:53,860
Any word on when we get it.

114
00:07:53,880 --> 00:07:56,680
We have to cast it to the type that we want.

115
00:07:57,060 --> 00:08:05,660
And then I'm going to go ahead and use the no coalescing to supply a default value if there isn't one.

116
00:08:05,700 --> 00:08:11,700
And when I say anonymous if there is no value then I'm going to go to the next one which is timestamps

117
00:08:11,770 --> 00:08:21,810
and say Let time stamp equal data and it's key is also time stamp.

118
00:08:21,810 --> 00:08:24,450
And that is a date.

119
00:08:24,780 --> 00:08:25,340
OK.

120
00:08:25,440 --> 00:08:31,590
And if there isn't a date then we'll go ahead and just initialize it to be the date of now which is

121
00:08:31,590 --> 00:08:32,910
fine.

122
00:08:32,940 --> 00:08:39,760
All right and then let's see if we can save some time by copying and pasting the user name.

123
00:08:39,870 --> 00:08:46,700
Since most of them are strings in case so let the next one is a thought text.

124
00:08:47,810 --> 00:08:52,920
And its key is thought to text and it is a string.

125
00:08:52,970 --> 00:08:59,840
If there isn't anything we're just going to do an empty string and then next we're going to say let

126
00:09:00,550 --> 00:09:11,660
some likes equal to the data and the key is numb likes and you know what I just realized I forgot that

127
00:09:11,660 --> 00:09:13,390
we have constants for all of these.

128
00:09:13,490 --> 00:09:16,400
So this should actually be user name.

129
00:09:16,460 --> 00:09:20,140
This should be a time stamp.

130
00:09:20,330 --> 00:09:27,530
This should be thought text and this should be numb likes.

131
00:09:27,530 --> 00:09:28,210
There you go.

132
00:09:28,460 --> 00:09:31,620
And this should be cast as it is.

133
00:09:31,630 --> 00:09:32,140
No.

134
00:09:32,210 --> 00:09:32,800
No.

135
00:09:32,810 --> 00:09:37,030
Then we'll set it to default zero when we need it.

136
00:09:37,050 --> 00:09:38,850
No comments.

137
00:09:38,890 --> 00:09:40,750
People turn.

138
00:09:41,330 --> 00:09:49,430
And it's key is numb underscore comments and that we shall set our cast as an int and if there isn't

139
00:09:49,430 --> 00:09:52,960
one will say that as a default value to zero.

140
00:09:53,000 --> 00:10:02,930
And then lastly we need the document ID document ID equal to and this is going to be a document that

141
00:10:03,470 --> 00:10:04,390
document ID.

142
00:10:04,400 --> 00:10:04,900
OK.

143
00:10:05,060 --> 00:10:11,240
So to get the document ID all we have to do is grab the document that is inside of snapshot documents

144
00:10:11,330 --> 00:10:13,660
and get the document ID property from it.

145
00:10:13,700 --> 00:10:14,900
Pretty easy.

146
00:10:14,960 --> 00:10:21,860
All right so now let's go ahead and initialize a thought from our thought class and then what we're

147
00:10:21,860 --> 00:10:26,570
going to do is we are going to append it to our thoughts right here.

148
00:10:27,080 --> 00:10:29,620
And then guess what we're just going to reload our table view.

149
00:10:29,690 --> 00:10:34,150
And we should see all of these thoughts being displayed in our table view.

150
00:10:34,160 --> 00:10:36,100
All right so we're actually pretty close here.

151
00:10:36,260 --> 00:10:42,240
So I'm going to say let new thought equal and construct a new thought from our class.

152
00:10:42,280 --> 00:10:45,050
Initialiser And here we go.

153
00:10:45,050 --> 00:10:49,000
So user name is equal to user name.

154
00:10:49,000 --> 00:10:58,880
Timestamp is equal to time stamp thought text is equal to last year thought text to thought text number

155
00:10:58,880 --> 00:11:09,640
of likes is numb likes comments is Gnome comments and document ID is document ID.

156
00:11:09,650 --> 00:11:17,300
All right so now we have this new thought we're going to say thoughts that are penned and the new element

157
00:11:17,300 --> 00:11:19,990
that we are spending is this new thought.

158
00:11:22,320 --> 00:11:29,860
Already And now that that is done what we're going to do is going to say table you dot reload data you're

159
00:11:29,890 --> 00:11:31,580
going to see this.

160
00:11:31,810 --> 00:11:37,920
And if it's here we need our selfs on here since we inside of a closure.

161
00:11:39,750 --> 00:11:42,980
And if I didn't forget anything which is improbable.

162
00:11:43,110 --> 00:11:44,500
But we'll see.

163
00:11:44,640 --> 00:11:48,330
Then we should be able to see these thoughts being displayed in our table views so let's run it and

164
00:11:48,330 --> 00:11:54,490
see you know this isn't it.

165
00:11:55,000 --> 00:11:56,050
Let's see here.

166
00:11:56,050 --> 00:12:02,620
These are all of our static table view cells that see oh you know what we forgot in our thought.

167
00:12:02,650 --> 00:12:09,460
So we forgot to actually do the configuring of the cell so we did everything else but we actually forgot

168
00:12:09,460 --> 00:12:12,530
the final step which is to configure the cell.

169
00:12:12,550 --> 00:12:13,290
All right so let's do it.

170
00:12:13,290 --> 00:12:15,750
We pass into this function a cell.

171
00:12:15,760 --> 00:12:24,970
So it's a user name label that text is equal to the thought that we pass into it and we access the username.

172
00:12:25,000 --> 00:12:25,930
There we go.

173
00:12:26,110 --> 00:12:33,370
Then time stamp label time stamp label that text is equal to fat dot the time stamp.

174
00:12:33,370 --> 00:12:47,200
Then a thought text label that text as you do thought dot dot text and likes no label that text is equal

175
00:12:47,200 --> 00:12:48,290
to.

176
00:12:48,290 --> 00:12:53,950
We have to do some string interpolation here let's say.

177
00:12:53,950 --> 00:13:02,730
Or do we need to let's say strings or string and thought that name likes.

178
00:13:02,740 --> 00:13:03,530
There we go.

179
00:13:03,880 --> 00:13:09,390
And here we're trying to say can I assign a value of type data type string.

180
00:13:09,640 --> 00:13:13,570
For now let's actually just leave this alone.

181
00:13:14,410 --> 00:13:18,370
So I'm going to save this and run it and see OK.

182
00:13:18,380 --> 00:13:19,700
There we go.

183
00:13:19,730 --> 00:13:27,060
Now we're actually seeing the data from our database that is actually pretty exciting.

184
00:13:27,240 --> 00:13:30,190
And man it looks pretty good doesn't it.

185
00:13:30,920 --> 00:13:32,630
All right well done.

186
00:13:32,630 --> 00:13:36,440
So let's just recap the flow of how this is all working.

187
00:13:36,440 --> 00:13:44,900
So here in our main VCM view will appear we are getting a reference to our collection then we're saying

188
00:13:45,020 --> 00:13:46,290
get documents.

189
00:13:46,430 --> 00:13:47,590
And so we are fetching.

190
00:13:47,600 --> 00:13:52,180
This is a one time fetch we're fetching all of the documents in a snapshot.

191
00:13:52,270 --> 00:13:52,860
OK.

192
00:13:53,240 --> 00:14:00,230
Then we we we do an unwrapped on the snapshot calling it snap and then we look through all of the documents

193
00:14:00,290 --> 00:14:01,860
inside of that snapshot.

194
00:14:02,120 --> 00:14:08,900
And so each document has data attached with it which is everything that we have here in our database

195
00:14:09,230 --> 00:14:12,820
and it is returned to us in a dictionary.

196
00:14:13,010 --> 00:14:18,830
And so all we have to do then is extract that information that we need from the dictionary construct

197
00:14:18,920 --> 00:14:27,890
a new thought from our thought class append that thought to our thoughts array right here and then we

198
00:14:27,950 --> 00:14:30,770
reload the table view data.

199
00:14:31,010 --> 00:14:38,310
This calls our table view methods which then loops through and using our thoughts array as the data

200
00:14:38,310 --> 00:14:39,450
source.

201
00:14:39,620 --> 00:14:42,440
We go ahead and configure the cells pass into it.

202
00:14:42,500 --> 00:14:47,630
The thought that corresponds to that cell in the in the index path rope.

203
00:14:47,810 --> 00:14:55,610
And here in that cell we configure the cell set to the IB outlets for each individual cell based on

204
00:14:55,610 --> 00:14:59,950
the thought that is passed into it and the end result is what you see here.

205
00:14:59,960 --> 00:15:00,190
All right.

206
00:15:00,200 --> 00:15:02,600
So that's that's pretty cool.

207
00:15:02,600 --> 00:15:06,590
All right so I think we're going to end this lesson right here in the next lesson.

208
00:15:06,590 --> 00:15:14,090
We're going to go over how to set up listeners so that we can see changes in real time as well as sorting

209
00:15:14,090 --> 00:15:14,790
and filtering.

210
00:15:14,790 --> 00:15:15,130
All right.

211
00:15:15,170 --> 00:15:17,300
So up here except for that and I'll see you in the next one.
