1
00:00:04,740 --> 00:00:05,900
Welcome back everyone.

2
00:00:05,940 --> 00:00:08,090
Johnny be here with slopes dot com.

3
00:00:08,280 --> 00:00:14,480
And this lesson we are going to finally make our first data entry into our fire store database are I.

4
00:00:14,640 --> 00:00:20,820
The last lesson we got Are you all set up so that we can select a category and a user name and write

5
00:00:20,820 --> 00:00:23,790
a random thought and then push this button to post it.

6
00:00:23,790 --> 00:00:25,100
Right now it's not doing anything though.

7
00:00:25,140 --> 00:00:26,200
So let's change that.

8
00:00:26,200 --> 00:00:26,680
All right.

9
00:00:26,910 --> 00:00:34,380
So we are going to first go to our fire base console or so open up your browser and head on over to

10
00:00:34,380 --> 00:00:39,490
your dashboard for the project that we created a couple of lessons ago.

11
00:00:39,490 --> 00:00:40,160
OK.

12
00:00:40,350 --> 00:00:42,480
So you should be here on the overview.

13
00:00:42,840 --> 00:00:45,500
And we're we're going to go we we're going to set up our database.

14
00:00:45,600 --> 00:00:48,690
Right so come over here to database.

15
00:00:48,900 --> 00:00:54,820
Now the previous version of the firebase database system is the real time database.

16
00:00:54,960 --> 00:01:00,480
And as we covered in the previous lesson a couple lessons ago there are still times when you would want

17
00:01:00,480 --> 00:01:05,790
to use a real time database but this lesson these lessons are all about using cloud firestorm.

18
00:01:06,020 --> 00:01:13,620
So we're going to click here on try fyrst or Bayda and we have some security rules for cloud store so

19
00:01:13,620 --> 00:01:18,710
we can start out in lockdown mode which makes your database private by denying all reads and writes.

20
00:01:18,750 --> 00:01:20,630
Or we can start in test mode.

21
00:01:20,790 --> 00:01:26,340
And this says get set up quickly by allowing all reads and writes to your database but you'll see here

22
00:01:26,340 --> 00:01:31,570
a warning that says anyone with your database reference will be able to read or write to your database.

23
00:01:31,710 --> 00:01:35,620
So this isn't particularly safe but just to get started.

24
00:01:35,700 --> 00:01:41,460
And for this course we're going to go ahead and just do this in test mode so that everything is open

25
00:01:41,460 --> 00:01:42,650
to read and write.

26
00:01:42,780 --> 00:01:48,900
But again just to reiterate don't do this for production apps or this is just to get started and to

27
00:01:48,930 --> 00:01:49,260
learn.

28
00:01:49,260 --> 00:01:49,830
All right.

29
00:01:50,100 --> 00:01:51,210
And future target topic.

30
00:01:51,210 --> 00:01:57,000
We are going to actually go over using firebase authentication and how to log in and how to set up those

31
00:01:57,000 --> 00:01:58,500
security rules in a better way.

32
00:01:58,500 --> 00:01:59,100
All right.

33
00:01:59,370 --> 00:02:02,720
All right so here we are in our fire store database.

34
00:02:02,760 --> 00:02:07,620
Now if you remember from a couple of lessons ago when we were going over the fire store basics at the

35
00:02:07,620 --> 00:02:12,270
root of our database we always have to have a collection.

36
00:02:12,270 --> 00:02:12,690
All right.

37
00:02:12,810 --> 00:02:18,750
And then inside of that collection we have our documents and our documents they can hold a number of

38
00:02:19,080 --> 00:02:26,100
different types of data including strings booleans numbers dates gnoll et cetera and well actually see

39
00:02:26,100 --> 00:02:27,560
that here in just a second.

40
00:02:27,870 --> 00:02:33,690
All right and so we can actually manually create collections and documents right here from our dashboard

41
00:02:33,690 --> 00:02:34,160
OK.

42
00:02:34,290 --> 00:02:38,830
So if I click here we're going to say at collection and the collection name is going to be.

43
00:02:38,880 --> 00:02:39,890
Thoughts.

44
00:02:40,110 --> 00:02:40,850
OK.

45
00:02:41,250 --> 00:02:45,030
And then we're going to need some documents.

46
00:02:45,030 --> 00:02:47,600
Let's go ahead and create our first document here.

47
00:02:47,610 --> 00:02:52,440
Now you we can specify a document I.D. we could say something like.

48
00:02:52,440 --> 00:02:54,360
First thought

49
00:02:57,130 --> 00:03:03,550
and that would be fine and then we could add our fields and everything or we could say auto ID and then

50
00:03:03,550 --> 00:03:11,400
it will automatically generate just randomly generated document ID and then here we have the fields

51
00:03:11,410 --> 00:03:11,880
OK.

52
00:03:12,010 --> 00:03:17,110
So these documents they are organized in kind of like you could think of like dictionaries in Iowa or

53
00:03:17,120 --> 00:03:18,310
Jason objects.

54
00:03:18,460 --> 00:03:22,370
You have a document and then inside of it you have key value pairs.

55
00:03:22,420 --> 00:03:26,450
However with fire store we call those keys instead we call them fields.

56
00:03:26,560 --> 00:03:28,970
And then the values are still values.

57
00:03:29,320 --> 00:03:34,430
So for our documents these are going to be our thoughts that we display.

58
00:03:34,570 --> 00:03:41,260
And if we open up our preview for our mockup we kind of take a look at one of these thoughts that are

59
00:03:41,260 --> 00:03:41,860
displayed.

60
00:03:41,860 --> 00:03:47,700
We kind of get an idea of what we need to have stored in these documents.

61
00:03:47,770 --> 00:03:49,590
We're going to need a user name.

62
00:03:49,660 --> 00:03:51,780
We're going to need a time stamp.

63
00:03:51,850 --> 00:03:54,550
We're going to need the thought text itself.

64
00:03:55,060 --> 00:04:01,360
We're going to need to know how many likes each thought has and then we're also going to put in a field

65
00:04:01,360 --> 00:04:07,600
for a number of comments and this will be for in the future when we implement comments on each of our

66
00:04:07,660 --> 00:04:08,740
Random thoughts.

67
00:04:08,740 --> 00:04:10,450
That's going to be useful then.

68
00:04:10,450 --> 00:04:17,050
All right so let's go ahead and jump into here back into our dashboard forecourts for firebase and let's

69
00:04:17,470 --> 00:04:19,670
manually create our first thought.

70
00:04:19,690 --> 00:04:20,130
OK.

71
00:04:20,320 --> 00:04:26,370
So here we have the field and we're going to call this category and it's going to be of type string.

72
00:04:26,650 --> 00:04:30,210
And for the value I'm just going to initialize it to be funny.

73
00:04:30,460 --> 00:04:30,830
OK.

74
00:04:30,850 --> 00:04:37,800
And then we can add another field and this one we're going to say and some comments for number of comments

75
00:04:38,300 --> 00:04:42,990
and this type is going to be of number and we'll initialize it to zero.

76
00:04:43,040 --> 00:04:49,010
I'll say add a field and then this one is going to be the number of likes that The Post has that's going

77
00:04:49,010 --> 00:04:50,100
to be number likes.

78
00:04:50,180 --> 00:04:55,840
And that's also going to be a number and we'll set that to zero and then we're going to create.

79
00:04:56,030 --> 00:04:58,930
So this one is going to be the text for the thought.

80
00:04:58,970 --> 00:05:05,240
So it's a thought text thought T T S T and we're going to write.

81
00:05:05,250 --> 00:05:10,080
This is a random thought and it is of type string.

82
00:05:10,930 --> 00:05:14,490
And then we're going to need a time stamp.

83
00:05:14,800 --> 00:05:23,040
And this one is of Ty time stamp and you can see here all of the different types that you can assign

84
00:05:23,040 --> 00:05:30,180
for a field if string number Boolean object array Noall timestamp G.O. point and reference.

85
00:05:30,180 --> 00:05:30,710
OK.

86
00:05:31,110 --> 00:05:36,150
So those are all some very useful things that you could add came the time stamp was go ahead and just

87
00:05:36,660 --> 00:05:38,550
set a date.

88
00:05:38,610 --> 00:05:46,810
Can we just leave that as zeros then we're going to have our final field which is user name.

89
00:05:47,160 --> 00:05:49,800
And then all right Johnny B.

90
00:05:50,250 --> 00:05:51,790
And we can save this.

91
00:05:52,260 --> 00:05:56,980
And now we have our very first let's see what's going on.

92
00:05:56,980 --> 00:06:02,250
I'm going just going to refresh this.

93
00:06:02,250 --> 00:06:02,580
There we go.

94
00:06:02,580 --> 00:06:03,340
That's better.

95
00:06:03,510 --> 00:06:06,970
All right so now we have our thought document.

96
00:06:07,140 --> 00:06:12,900
And so you see here that it follows the pattern collection collection his thoughts and inside of that

97
00:06:12,990 --> 00:06:17,680
thoughts collection we have a number of documents and then we have the document.

98
00:06:17,700 --> 00:06:23,990
And here are its fields and then if you wanted you could have another subcollection to this document.

99
00:06:24,120 --> 00:06:28,260
And inside of that subcollection you would have other documents and.

100
00:06:28,350 --> 00:06:29,670
And so on and so forth.

101
00:06:29,670 --> 00:06:33,380
So always following the pattern collection document collection document.

102
00:06:33,600 --> 00:06:40,500
So in a future target topic when we implement comments for each of these thoughts then each of these

103
00:06:40,620 --> 00:06:47,130
documents is going to have a subcollection of comments and each of those comments will be a document

104
00:06:47,130 --> 00:06:48,960
inside of that subcollection.

105
00:06:48,960 --> 00:06:49,200
All right.

106
00:06:49,210 --> 00:06:50,370
So some pretty cool stuff.

107
00:06:50,580 --> 00:06:52,050
All right so yeah we have rent.

108
00:06:52,050 --> 00:06:56,690
We have manually placed our very first thought in our database.

109
00:06:56,690 --> 00:07:00,480
That's pretty cool but not nearly as cool as actually doing it from our app.

110
00:07:00,480 --> 00:07:00,930
All right.

111
00:07:01,050 --> 00:07:04,470
So let's go ahead and make it so that we can do that from the app.

112
00:07:04,470 --> 00:07:11,690
All right so we're going to jump back into X code and this is where we are going to do it right here

113
00:07:11,690 --> 00:07:13,650
in the post button tapped.

114
00:07:13,650 --> 00:07:16,400
All right so let's do a little bit of prep work.

115
00:07:16,400 --> 00:07:24,080
First we're going to need to be able to have a way of setting the category and getting the thought text

116
00:07:24,080 --> 00:07:24,930
and the user name.

117
00:07:24,920 --> 00:07:25,430
All right.

118
00:07:25,670 --> 00:07:27,830
So let's create a variable here.

119
00:07:27,840 --> 00:07:33,080
This variable is going to be called when I make a little header for variables but I'm going to spell

120
00:07:33,080 --> 00:07:34,130
it correctly.

121
00:07:34,180 --> 00:07:42,310
The variables that we go and when you create a variable here are going to save private var.

122
00:07:42,500 --> 00:07:45,770
I'm going to call it selected category.

123
00:07:45,790 --> 00:07:46,290
OK.

124
00:07:46,460 --> 00:07:52,640
And this is going to keep track of which category is selected when we go ahead and send them to get

125
00:07:52,640 --> 00:07:53,910
my simulated back up here.

126
00:07:54,020 --> 00:07:59,160
When we click on the and we select the different types of thoughts that we can post.

127
00:07:59,170 --> 00:07:59,580
All right.

128
00:07:59,630 --> 00:08:04,240
And I'm going to just initialize this too funny to start out with.

129
00:08:04,570 --> 00:08:05,680
That looks good.

130
00:08:05,870 --> 00:08:10,880
And then we were going to need to import firebase houmous to import firebase and there we go.

131
00:08:11,060 --> 00:08:14,330
So it doesn't matter which SDK you're working with.

132
00:08:14,330 --> 00:08:19,280
All you need to do is import firebase and that will cover the whole umbrella of different STK that you

133
00:08:19,280 --> 00:08:20,570
could be working with.

134
00:08:20,960 --> 00:08:27,620
All right so now we're here when we post on the button tapped how it works when we are saving data to

135
00:08:27,650 --> 00:08:34,340
our online database where we need to do is we need to get a reference to our collection or our document

136
00:08:34,340 --> 00:08:36,080
that we want to write to.

137
00:08:36,370 --> 00:08:42,630
And there are a couple of ways of doing that we could say fire store that fire store.

138
00:08:42,890 --> 00:08:43,260
OK.

139
00:08:43,340 --> 00:08:46,520
And then we need a reference to our base collection.

140
00:08:46,520 --> 00:08:46,980
OK.

141
00:08:47,210 --> 00:08:50,070
And so we can say collection path right here.

142
00:08:50,090 --> 00:08:55,790
And this is this gets a fire collection reference referring to the collection at the specified path

143
00:08:55,820 --> 00:08:57,170
within the database.

144
00:08:57,230 --> 00:08:57,750
OK.

145
00:08:58,070 --> 00:09:01,540
And our collection is called thoughts.

146
00:09:01,660 --> 00:09:02,180
OK.

147
00:09:02,570 --> 00:09:10,420
So this right here this gets us a reference directly to this collection here in our fire store database.

148
00:09:10,420 --> 00:09:10,930
OK.

149
00:09:10,940 --> 00:09:18,350
And then once we have that what we can do is we can say Dot and document and so here we can see we have

150
00:09:18,350 --> 00:09:21,520
had document and add document with a completion handler.

151
00:09:21,740 --> 00:09:25,470
OK so if we did this one then we wouldn't have any way of knowing once it was complete.

152
00:09:25,550 --> 00:09:31,010
But we want to know when it's complete so that we can segue back to our initial controller as well as

153
00:09:31,010 --> 00:09:32,830
Hendel if there are any errors.

154
00:09:33,130 --> 00:09:37,910
Ok so I'm going to select the on the completion handler we go and then I'm just going to press return

155
00:09:37,910 --> 00:09:44,540
again and then I'm going to tab and press return again to get the completion handler then to rename

156
00:09:44,630 --> 00:09:47,670
this error to just air e r r.

157
00:09:48,130 --> 00:09:48,640
OK.

158
00:09:48,770 --> 00:09:54,470
And then just the area of this area quick I'm just going to put empty strings are right here.

159
00:09:54,500 --> 00:09:55,240
Looks good.

160
00:09:55,580 --> 00:09:59,770
And then right here in the code this is where we can handle the error.

161
00:09:59,780 --> 00:10:07,610
So we're going to say if the air is equal to air so if there is an error and then we're going to sit

162
00:10:07,640 --> 00:10:10,840
equal to air and we're just going to debug printers to say Debug.

163
00:10:10,840 --> 00:10:16,850
Print and I'm going to say error adding document

164
00:10:19,700 --> 00:10:24,300
and then we're going to print out the error and say error.

165
00:10:24,650 --> 00:10:25,520
OK.

166
00:10:25,940 --> 00:10:34,520
Else if we were successful then I'm going to say self that navigation controller and we're just going

167
00:10:34,520 --> 00:10:38,160
to pop this view controller and we're going to stay true.

168
00:10:38,300 --> 00:10:44,210
So when you're using a navigation control you can use this method poppier you controller to just go

169
00:10:44,210 --> 00:10:47,710
backwards to the previous controller in the stack.

170
00:10:47,870 --> 00:10:48,560
OK.

171
00:10:48,860 --> 00:10:51,200
All right so now it's come back up here to our data.

172
00:10:51,260 --> 00:10:56,180
All right so we are saying add a document and the document that we are adding is this data OK.

173
00:10:56,360 --> 00:11:01,520
So we're going to do here is we are going to construct a dictionary that contains all of the data that

174
00:11:01,520 --> 00:11:04,440
we want to send up to our database.

175
00:11:04,630 --> 00:11:10,700
So we're going to have a dictionary of key and values that contain the information that we want to send

176
00:11:10,700 --> 00:11:11,420
up.

177
00:11:11,420 --> 00:11:15,300
So for instance we have up here and this is a data dictionary.

178
00:11:15,330 --> 00:11:15,880
OK.

179
00:11:16,070 --> 00:11:18,500
So we're going to have key value pairs for each one.

180
00:11:18,700 --> 00:11:19,220
OK.

181
00:11:19,340 --> 00:11:20,650
So we're in the category.

182
00:11:20,750 --> 00:11:26,810
So here I'm going to write the category and the string that we're going to be setting up.

183
00:11:26,810 --> 00:11:29,850
This is the selected category here.

184
00:11:29,840 --> 00:11:31,250
So when I say selected

185
00:11:33,840 --> 00:11:34,530
category

186
00:11:37,130 --> 00:11:42,510
get and then just to make this a little bit easier to read I'm going to write after this quick break

187
00:11:42,540 --> 00:11:48,360
here I'm going to do a return and then right here after I select the category I'm going to do another

188
00:11:48,360 --> 00:11:53,610
return tape and then after select category I'm going to do a comma because we have a number of things

189
00:11:53,610 --> 00:11:55,110
that we're going to add here.

190
00:11:55,140 --> 00:12:00,640
All right and so the next key value pair is numb comments.

191
00:12:00,660 --> 00:12:07,260
OK so this is going to be the field name that we send up in our document and it's value is going to

192
00:12:07,260 --> 00:12:09,240
initialize that to zero.

193
00:12:09,670 --> 00:12:19,260
Then when her next fuel the value pair is numb comments and its value is also going to be initialized

194
00:12:19,260 --> 00:12:22,520
to zero and then we need the thought text.

195
00:12:22,760 --> 00:12:23,260
OK.

196
00:12:23,460 --> 00:12:24,650
So let's see.

197
00:12:24,660 --> 00:12:30,350
This is going to be thought text and it is.

198
00:12:30,360 --> 00:12:33,110
So what did we call our label here thought text.

199
00:12:33,110 --> 00:12:33,740
All right.

200
00:12:33,840 --> 00:12:42,080
So we're just going to say thought text dupped text.

201
00:12:42,410 --> 00:12:47,270
And then the next one is a time stamp.

202
00:12:47,910 --> 00:12:48,150
OK.

203
00:12:48,170 --> 00:12:53,920
And for this one how we can initialize a time stamp is we say field

204
00:12:56,370 --> 00:13:04,380
value dot server time stamp and this is used with said data or update data to include a server generated

205
00:13:04,380 --> 00:13:06,220
time stamp in the written data OK.

206
00:13:06,310 --> 00:13:13,110
So we're just using this method to create the time stamp at the exact moment that we create this using

207
00:13:13,110 --> 00:13:14,270
the server time stamp.

208
00:13:14,400 --> 00:13:15,040
OK.

209
00:13:15,420 --> 00:13:17,590
And then lastly we need the user name.

210
00:13:17,600 --> 00:13:17,960
OK.

211
00:13:18,000 --> 00:13:27,340
So user name and the value is and this is going to be the user name.

212
00:13:27,450 --> 00:13:33,230
Text that text and that for now will just force unwrap this with a bang.

213
00:13:33,930 --> 00:13:34,260
All righty.

214
00:13:34,280 --> 00:13:35,380
And that's it.

215
00:13:35,400 --> 00:13:42,060
We have created our function that is going to send up all of the all of this data to fire store and

216
00:13:42,060 --> 00:13:44,420
create a brand new documentary.

217
00:13:44,820 --> 00:13:51,270
So just to recap what we did is we got a reference to our collection by saying fire store fire store

218
00:13:51,480 --> 00:13:55,580
data collection and we pass in the name of the collection here.

219
00:13:55,770 --> 00:13:57,150
And then we see ad document.

220
00:13:57,150 --> 00:14:04,170
So we're going to be adding a document to our collection of thoughts and then we set the data that is

221
00:14:04,170 --> 00:14:07,610
going to construct the document that we are sending up.

222
00:14:07,680 --> 00:14:10,260
And this is a dictionary of key value pairs.

223
00:14:10,290 --> 00:14:16,020
And so we set all of the keys and all of the values and then we have a completion handler where we can

224
00:14:16,020 --> 00:14:21,240
handle the error if there is one and we just debug printing it for now.

225
00:14:21,330 --> 00:14:26,430
And if it's successful then we are going to pop back to our main visi.

226
00:14:26,430 --> 00:14:27,430
All right let's go ahead and test this.

227
00:14:27,440 --> 00:14:33,720
I want to run it right so I have the app here and then I'm going to open a bar Safari window as well

228
00:14:33,990 --> 00:14:37,440
so that we can see if this automatically pops up here.

229
00:14:37,560 --> 00:14:43,890
So I'm going to go to our plus button here I'm going to click on it and I got also a collection of kind

230
00:14:43,890 --> 00:14:47,330
of just some silly random thoughts that I got from the internet.

231
00:14:47,430 --> 00:14:51,000
I want to copy that here and I'm going to paste it in here.

232
00:14:51,030 --> 00:14:52,310
Let's see here.

233
00:14:52,540 --> 00:14:52,780
OK.

234
00:14:52,800 --> 00:14:57,220
And then the user name is soil work fanatic.

235
00:14:57,300 --> 00:14:59,420
All right so these are from Reddit by the way.

236
00:14:59,730 --> 00:15:06,090
And then I'm going to say post and if all goes well we should see a newly generated Auto-ID pop appear

237
00:15:06,090 --> 00:15:08,790
with all of the information that we set.

238
00:15:08,790 --> 00:15:09,000
All right.

239
00:15:09,000 --> 00:15:13,980
So when I say post oh we got an error.

240
00:15:14,010 --> 00:15:16,250
All right let's see what's going on here.

241
00:15:16,290 --> 00:15:21,410
You'll see here that we have a fatal error dictionary literal contains duplicate keys.

242
00:15:21,420 --> 00:15:24,340
That's because I accidentally wrote comments twice in a row.

243
00:15:25,450 --> 00:15:28,750
The second one should be numb this.

244
00:15:29,030 --> 00:15:31,750
Never go so let's try that again.

245
00:15:31,750 --> 00:15:37,590
All right then let's get our database back open and are happy and let's try that one more time.

246
00:15:37,630 --> 00:15:37,870
OK.

247
00:15:37,870 --> 00:15:45,060
So here I'm going to pace to that random thought back in the paste and the user name was Soilwork phonetic.

248
00:15:45,220 --> 00:15:52,090
OK and let's try that one more time post umu Hey and we dismiss back to our initial Visi And here in

249
00:15:52,090 --> 00:15:54,760
our collection we have a brand new document.

250
00:15:54,790 --> 00:16:00,040
If we click on it sure enough we can see the category the number of comments number of likes the thought

251
00:16:00,040 --> 00:16:03,030
text and the time stamp and the user name.

252
00:16:03,040 --> 00:16:04,940
All right so that is pretty cool.

253
00:16:04,960 --> 00:16:06,450
Let's let's let's do another one.

254
00:16:06,670 --> 00:16:08,530
Let's come back to our ad post.

255
00:16:08,530 --> 00:16:15,070
Make sure this is still working and that someone can do a post by Jack Handy.

256
00:16:15,070 --> 00:16:17,510
He was a character on SNL for a while.

257
00:16:17,500 --> 00:16:23,810
Super funny guy and he had a collection of funny sayings called simple thoughts.

258
00:16:24,040 --> 00:16:26,320
And so I'm going to paste one of those here.

259
00:16:26,320 --> 00:16:26,600
All right.

260
00:16:26,620 --> 00:16:31,730
And so let's post that again and check out over here make sure it pops up when we get it.

261
00:16:31,730 --> 00:16:34,390
So we've got a new one and see here.

262
00:16:34,590 --> 00:16:35,450
Yep there we go.

263
00:16:35,500 --> 00:16:37,210
So we've got the category number of likes.

264
00:16:37,210 --> 00:16:38,040
Everything.

265
00:16:38,110 --> 00:16:38,290
All right.

266
00:16:38,290 --> 00:16:39,160
Very nice.

267
00:16:39,220 --> 00:16:46,810
So you can see how simple firestorm makes it to post documents to our database.

268
00:16:46,810 --> 00:16:46,990
All right.

269
00:16:46,990 --> 00:16:53,630
One thing that I want to touch on here is that we get a reference to our collection called thoughts.

270
00:16:53,710 --> 00:17:00,180
But if this this collection didn't even exist then none of these documents existed.

271
00:17:00,180 --> 00:17:00,570
We.

272
00:17:00,580 --> 00:17:03,460
And so this was the very first time we were making a post.

273
00:17:03,520 --> 00:17:06,580
We don't have to manually create a collection called thoughts.

274
00:17:06,580 --> 00:17:12,730
If we say fire store dust collection thoughts and we had a document that will implicitly create the

275
00:17:12,730 --> 00:17:14,730
collection called thoughts and the document.

276
00:17:14,730 --> 00:17:15,160
All right.

277
00:17:15,160 --> 00:17:20,530
So you don't have to manually create collections or documents and then add to them you just tell firebase

278
00:17:20,530 --> 00:17:24,420
that we want to create them and then if they don't exist they'll be created.

279
00:17:24,550 --> 00:17:28,290
If the collection exists then the document will be created inside of the collection.

280
00:17:28,300 --> 00:17:28,900
Right.

281
00:17:29,250 --> 00:17:30,610
All right so that's cool.

282
00:17:30,610 --> 00:17:34,460
All right so one last thing that I want to do to kind of clean this up a little bit.

283
00:17:34,480 --> 00:17:39,560
Right now we don't really have a way of setting the category that is selected.

284
00:17:39,790 --> 00:17:46,450
We have our action for when we go ahead and select one of the different categories but we're not saving

285
00:17:46,450 --> 00:17:47,430
that anywhere aren't.

286
00:17:47,650 --> 00:17:53,110
So I'm thinking is first let's create an enum an enumerator to store the different types of categories

287
00:17:53,110 --> 00:17:54,530
that we can select from.

288
00:17:54,730 --> 00:18:00,170
So let's do that here in our main Visi up here outside of the class declaration.

289
00:18:00,340 --> 00:18:04,700
Let's create an enumerate just means that num.

290
00:18:05,100 --> 00:18:11,740
And we're going to call that thought category and of type string.

291
00:18:12,570 --> 00:18:27,090
And our cases are going to be case serious and then string serious case funny and then string funny

292
00:18:28,240 --> 00:18:38,770
and then case crazy and then string crazy and lastly case popular because it's going to be one of the

293
00:18:38,770 --> 00:18:43,920
ways that we can sort the data and that is going to be popular.

294
00:18:44,400 --> 00:18:45,320
And there we go.

295
00:18:45,460 --> 00:18:50,410
And then the show back into our ad thought Visi and all we can do here is we're going to still initialize

296
00:18:50,410 --> 00:18:51,090
to funny.

297
00:18:51,100 --> 00:18:58,680
But instead of having just the string funny we're going to say thought category not funny that raw.

298
00:18:58,720 --> 00:19:03,060
OK so the real value gets the actual streaming value for the case.

299
00:19:03,340 --> 00:19:07,400
And then down here in our category change so each time that we click on one of those different segmented

300
00:19:07,420 --> 00:19:14,080
controllers for funny serious crazy we're going to set the value for our selected category to the correct

301
00:19:14,080 --> 00:19:14,840
one.

302
00:19:14,980 --> 00:19:16,390
So we're going to do a switch statement.

303
00:19:16,420 --> 00:19:23,070
We're going to see switch and the value that we're switching on is the category segment not select that

304
00:19:23,080 --> 00:19:23,890
segment index.

305
00:19:23,890 --> 00:19:27,470
Okay so I'm going to say category segment dot selected segment index.

306
00:19:27,460 --> 00:19:33,230
Okay so we're doing a switch statement for each time that we change the selection in our segment control.

307
00:19:33,480 --> 00:19:33,770
Okay.

308
00:19:33,770 --> 00:19:40,680
And so for the first case we're going to zero which is the funny cat and we're going to just say selected

309
00:19:40,690 --> 00:19:45,140
category is equal to thought category diet.

310
00:19:45,700 --> 00:19:46,090
Let's see here.

311
00:19:46,090 --> 00:19:48,910
Funny that raw value.

312
00:19:49,210 --> 00:19:52,860
And then for a case of one.

313
00:19:52,960 --> 00:19:59,890
So this is the second segment to control that's going to be you select the categories equal to thought

314
00:20:00,010 --> 00:20:04,540
category and serious not a raw value.

315
00:20:04,750 --> 00:20:11,230
And then we'll just set our default value to be the third segment and that is going to be selected categories

316
00:20:11,290 --> 00:20:17,640
equal to 4 category that crazy to raw value.

317
00:20:18,010 --> 00:20:21,740
So now each time that we click on one of those Let's get our simulator here.

318
00:20:21,860 --> 00:20:29,200
So now each time we click on one of these we are resetting the variable for the for the selected category

319
00:20:29,440 --> 00:20:33,780
to be a string of value for funny serious or crazy.

320
00:20:33,790 --> 00:20:34,720
All right that's good.

321
00:20:34,720 --> 00:20:40,390
All right so let's get rid of this being operator right here let's come up here and we're going to do

322
00:20:40,390 --> 00:20:51,070
a guard statement here we're going to say guard let user and name equal to user name text dot text else

323
00:20:51,190 --> 00:20:52,570
and we'll just return.

324
00:20:52,900 --> 00:20:53,610
OK.

325
00:20:54,190 --> 00:21:01,230
And then down here instead of using in text text we're going to say user name.

326
00:21:01,270 --> 00:21:06,700
Now one last thing that I'm not liking is we have all of these magic strings just lying around.

327
00:21:06,700 --> 00:21:12,010
What if I had accidentally mistyped one of these and we're going to be using these in other places.

328
00:21:12,010 --> 00:21:14,140
Well so let's go ahead and make these constants I think.

329
00:21:14,140 --> 00:21:24,340
So when we create a new group here a new group run call these utilities can it create a new file and

330
00:21:24,630 --> 00:21:29,950
treat us with file here and we're going to call this Constance or I and this is we're going to store

331
00:21:29,950 --> 00:21:35,610
all of our constants say create and we're going to have a constant for our thoughts collection case

332
00:21:35,620 --> 00:21:38,600
when we say let thoughts underscore ref.

333
00:21:38,620 --> 00:21:44,470
And it's common practice to use capitalized letters four to denote constants so say thought to underscore

334
00:21:44,980 --> 00:22:01,990
equal to thoughts and then let category equal to category and then let some comments equal to some comments

335
00:22:02,680 --> 00:22:22,120
and then let them likes equal to likes and then let thought text equal to thought text and then let

336
00:22:22,630 --> 00:22:27,250
time stamp equal to time stamp.

337
00:22:27,850 --> 00:22:35,590
And lastly let user name equal user name.

338
00:22:35,630 --> 00:22:41,340
So now we have a cup Constance for each of these field values and our thoughts reference.

339
00:22:41,420 --> 00:22:46,770
I'm going to save that and jump back into the sea and let's just go ahead and replace those categories.

340
00:22:46,800 --> 00:22:50,410
Now get to Ghori.

341
00:22:50,860 --> 00:22:53,020
Not sure why in my article it isn't working.

342
00:22:53,020 --> 00:23:03,010
But Num. comments is now numb underscore comments like likes is now underscoring the likes thought text

343
00:23:03,010 --> 00:23:13,490
is now thought underscore text the time stamp is now time stamp and user name is now a user name.

344
00:23:16,030 --> 00:23:16,300
All right.

345
00:23:16,300 --> 00:23:17,260
That looks good.

346
00:23:17,260 --> 00:23:18,250
Let me make sure.

347
00:23:18,280 --> 00:23:21,010
What do we have here for the thought text Constans.

348
00:23:21,410 --> 00:23:23,110
Oh that's because I spelled it wrong right here.

349
00:23:23,160 --> 00:23:23,670
OK.

350
00:23:23,830 --> 00:23:27,560
So you g h t just going to copy that.

351
00:23:27,790 --> 00:23:29,930
Come back to our I had thought.

352
00:23:31,120 --> 00:23:33,530
And replace that.

353
00:23:33,670 --> 00:23:34,590
All right so that's looking good.

354
00:23:34,590 --> 00:23:38,150
It's going to run this one more time up and we can we can replace this right here as well.

355
00:23:38,150 --> 00:23:39,420
So thoughts.

356
00:23:39,440 --> 00:23:39,960
Ref.

357
00:23:39,990 --> 00:23:41,780
There you go my autocomplete is back now.

358
00:23:41,910 --> 00:23:45,880
All right so let's go in and run this and make sure everything is still working as expected.

359
00:23:45,880 --> 00:23:47,080
All right so here we are.

360
00:23:47,200 --> 00:23:51,620
Let's add another random thought and just can put Johnny B.

361
00:23:51,940 --> 00:23:57,200
And I'm just going to write some random gibberish and say post.

362
00:23:57,700 --> 00:23:59,290
And here we go.

363
00:23:59,830 --> 00:24:02,790
Let's see here category and I'm calling some likes.

364
00:24:02,830 --> 00:24:06,730
All right everything is looking good.

365
00:24:06,730 --> 00:24:12,860
All right so congratulations you are now working with firestorm from your app adding data to your database.

366
00:24:12,940 --> 00:24:18,730
In the next lessons we are going to be covering some really cool stuff on how to retrieve that information

367
00:24:19,210 --> 00:24:23,660
how to filter it how to sort it how to add listeners for real time updates.

368
00:24:23,740 --> 00:24:25,580
And yes it's going to be really cool.

369
00:24:25,780 --> 00:24:30,610
First up though in the next lesson we're going to do a little bit more work so that we can set up our

370
00:24:30,610 --> 00:24:35,980
table view and have a place to be able to display that data that we're going to be working on retrieving.

371
00:24:35,980 --> 00:24:38,710
All right so I'll see you in the next one is going to be great and I'll see you then.
