1
00:00:04,500 --> 00:00:06,300
Hey everyone welcome back to class.

2
00:00:06,330 --> 00:00:08,570
Johnny be here with slopes dot com.

3
00:00:08,760 --> 00:00:13,890
And in this lesson we are going to add our first comments and in the process learn about fire store

4
00:00:13,890 --> 00:00:15,020
transactions.

5
00:00:15,020 --> 00:00:15,380
All right.

6
00:00:15,540 --> 00:00:17,280
So let's get started.

7
00:00:17,280 --> 00:00:21,660
First thing I want to do is just run this and make sure that our segues are working.

8
00:00:21,660 --> 00:00:23,580
We haven't tested that yet.

9
00:00:23,790 --> 00:00:27,130
So we are passing from our main these when we click on a thought.

10
00:00:27,150 --> 00:00:28,770
We were passing that thought here.

11
00:00:28,800 --> 00:00:38,350
So just to make sure that is working let's go ahead and print out thought that comment or thought text.

12
00:00:38,350 --> 00:00:43,470
Are you going to run that and when we click on one of the thoughts we should see printed out to our

13
00:00:43,470 --> 00:00:44,340
log.

14
00:00:44,340 --> 00:00:49,270
The thought that we couldn't get someone to click on this one.

15
00:00:49,380 --> 00:00:52,080
And all right so it looks like that is working.

16
00:00:52,080 --> 00:00:55,630
Don't give up on your dreams keep sleeping is printed out to our blog.

17
00:00:56,160 --> 00:01:00,090
And before I forget let's go ahead and fix up our table.

18
00:01:00,100 --> 00:01:04,800
I don't like these dividers and I know there's something else that we need to fix as well.

19
00:01:04,800 --> 00:01:12,690
So jumping into our main story board let's hear table you selected and separator should be none.

20
00:01:12,720 --> 00:01:17,040
And then on the Common to sell selection should be none.

21
00:01:17,070 --> 00:01:18,020
There we go.

22
00:01:18,450 --> 00:01:18,660
All right.

23
00:01:18,660 --> 00:01:21,450
So now back into our comments visi.

24
00:01:21,570 --> 00:01:29,500
So now that we have we know our Segway is working we have a thought variable here that we can reference.

25
00:01:29,520 --> 00:01:35,810
So let's talk a little bit about how the architecture for this is going to work.

26
00:01:35,820 --> 00:01:42,570
Now we know that entire store we have our data organized into collections and documents.

27
00:01:42,600 --> 00:01:48,090
Collection holds a bunch of documents and each of those documents could have some collections.

28
00:01:48,150 --> 00:01:55,020
So in this app we have thoughts a collection of thoughts and we have these documents that each of these

29
00:01:55,200 --> 00:02:02,970
documents can have a subcollection called the commons and inside of that collection we will have more

30
00:02:02,970 --> 00:02:08,410
documents that represent the comments that are added for that particular thought.

31
00:02:08,600 --> 00:02:11,090
OK so that's kind of how it's going to work.

32
00:02:11,760 --> 00:02:17,120
So first off let's go ahead and create a reference to the individual thought that is clicked on.

33
00:02:17,130 --> 00:02:26,650
So here I'm going to say who are thought ref and this is going to be of type document.

34
00:02:27,240 --> 00:02:28,360
Let's see here.

35
00:02:30,280 --> 00:02:32,210
Oh I don't think we have fire so that's why.

36
00:02:32,520 --> 00:02:37,020
So wasn't complaining because we don't have fires firebase imported yet.

37
00:02:37,080 --> 00:02:44,760
There we go document reference and implicitly and unwrap that and then here in view to load and when

38
00:02:44,840 --> 00:02:51,160
to say thought ref is equal to fire store.

39
00:02:51,440 --> 00:02:56,380
And actually we can create a reference for fire store so that we don't have to write out fire store

40
00:02:56,600 --> 00:03:03,950
fire store every time to save fire or is equal to a fire store that fires store.

41
00:03:04,370 --> 00:03:13,550
And so here thought Refco going to say fires door at the Capitol One their fire store that collection.

42
00:03:13,550 --> 00:03:13,850
All right.

43
00:03:13,850 --> 00:03:17,290
This is the reference to the fact that we selected.

44
00:03:17,390 --> 00:03:23,330
So looking at our database there would be the reference to one of these individual documents here.

45
00:03:23,470 --> 00:03:23,940
OK.

46
00:03:24,080 --> 00:03:35,360
So if I restore the collection the collection path is thoughts ref and then the document path is thought

47
00:03:36,860 --> 00:03:39,530
that document ID.

48
00:03:39,960 --> 00:03:47,330
So now we have a reference to the path to the thought that we can use throughout the rest of this class.

49
00:03:47,360 --> 00:03:54,700
Next thing I want to do in preparation is create a variable for a username Katulis a variable user name.

50
00:03:55,100 --> 00:04:02,150
And this is going to be a string and where we're going to get this is from our off.

51
00:04:02,150 --> 00:04:09,590
So if you remember a couple of lessons ago in our create user Visi we actually set a display name that

52
00:04:09,590 --> 00:04:12,700
we can access from our user.

53
00:04:12,770 --> 00:04:19,310
So here comments VC I'm going to say there's going to be an option or someone to say if that name is

54
00:04:19,400 --> 00:04:24,160
equal to off that off that current user.

55
00:04:24,230 --> 00:04:28,610
OK so this gets the cached current user or gnoll if there is none.

56
00:04:28,740 --> 00:04:30,290
OK so the current user.

57
00:04:30,620 --> 00:04:40,630
And then we access the display name and then we're going to say a user name is equal to name.

58
00:04:41,200 --> 00:04:42,090
Very good.

59
00:04:42,530 --> 00:04:45,620
All right so now let's get to our ad comment tapped button.

60
00:04:45,800 --> 00:04:51,420
OK so this is what it is called When we look to add a new comment.

61
00:04:51,830 --> 00:04:56,640
And so first up let's go ahead and get our comment tecs.

62
00:04:56,660 --> 00:05:04,440
So when we say God let comment text equal self-taught.

63
00:05:04,640 --> 00:05:11,260
I don't mean this right here and say what did we call it we called it add comment text.

64
00:05:11,360 --> 00:05:20,810
So add common text that text else return in case and now we have the text of the comment that we are

65
00:05:20,810 --> 00:05:24,090
adding in this bottom pane here.

66
00:05:24,110 --> 00:05:26,970
All right so now we are ready to add our comment.

67
00:05:27,020 --> 00:05:32,480
So previously we just use the firestorm method and document which has a completion handler where we

68
00:05:32,480 --> 00:05:36,260
add the data as a dictionary and then send it up.

69
00:05:36,440 --> 00:05:36,930
Right.

70
00:05:37,130 --> 00:05:38,830
And we could do that.

71
00:05:38,930 --> 00:05:44,120
But the reason that we're going to use something else which I'll explain here in a second is that we

72
00:05:44,120 --> 00:05:51,370
are also wanting to update the number of comments for this thought in our fire store database.

73
00:05:51,560 --> 00:05:59,570
So we're adding a document but we also need to be adding incrementing a value in the thought document

74
00:05:59,660 --> 00:06:00,730
as well.

75
00:06:00,890 --> 00:06:06,020
And so we would be doing multiple reads and writes and that's actually best handled in something called

76
00:06:06,020 --> 00:06:07,230
a transaction.

77
00:06:07,370 --> 00:06:13,640
OK so when a transaction you can group multiple operations into a single transaction transaction consists

78
00:06:13,640 --> 00:06:21,920
of any number of get operations followed by any number of right operations such as set update or delete.

79
00:06:22,250 --> 00:06:24,760
All right so why do we need a transaction here.

80
00:06:24,980 --> 00:06:30,500
Well just like I said we're going to be adding a comment as well as updating the field the value for

81
00:06:30,500 --> 00:06:31,760
the thought.

82
00:06:32,000 --> 00:06:34,390
But a couple of things to keep in mind.

83
00:06:34,490 --> 00:06:38,990
For a transaction I read operations must come before right operations.

84
00:06:39,110 --> 00:06:44,090
So first we're going to read from the thought we're going to get the number of comments that it currently

85
00:06:44,090 --> 00:06:44,800
has.

86
00:06:45,050 --> 00:06:50,960
And then we are going to update that field as well as add the new comment for it.

87
00:06:50,960 --> 00:06:54,230
Rule number two transactions never apply partial rights.

88
00:06:54,260 --> 00:06:58,790
So it's an all or nothing deal and transactions fail when user is off line.

89
00:06:58,790 --> 00:07:04,520
So that's something to keep in mind and a transaction should not directly modify the application state

90
00:07:04,580 --> 00:07:09,290
meaning that this transaction is not executed on the main thread.

91
00:07:09,350 --> 00:07:12,860
With that all out of the way let's go ahead and code it up.

92
00:07:13,160 --> 00:07:25,160
So to do so we just call fire store that run transaction and press return and we have a handler here

93
00:07:25,600 --> 00:07:31,100
and we have a transaction and then error pointer.

94
00:07:31,750 --> 00:07:35,680
And then here in the code and then we have another question handler here.

95
00:07:35,920 --> 00:07:42,960
And this is we're just going to call this object and error in code.

96
00:07:43,220 --> 00:07:48,070
OK so this transaction is what we're going to be working with here inside of the actual running of the

97
00:07:48,070 --> 00:07:48,940
transaction.

98
00:07:49,000 --> 00:07:53,070
And then here is an object that could be returned out of the transaction.

99
00:07:53,280 --> 00:07:54,300
OK.

100
00:07:54,620 --> 00:07:57,690
So go ahead and just say return nil.

101
00:07:57,790 --> 00:08:00,440
We're not going to be returning anything out of the transaction.

102
00:08:00,910 --> 00:08:04,320
All right so remember that reads need to occur before rights.

103
00:08:04,330 --> 00:08:11,350
So first we're going to get the thought document so that we can know how many number of comments there

104
00:08:11,350 --> 00:08:14,870
are so that we can add one to it when we add this comment.

105
00:08:15,010 --> 00:08:15,390
OK.

106
00:08:15,520 --> 00:08:27,370
So I'm going to create a thought document here type document snapshot and I need to declare it here

107
00:08:27,610 --> 00:08:32,540
because we access it in a try block which I'll write out here.

108
00:08:32,580 --> 00:08:33,050
OK.

109
00:08:33,190 --> 00:08:51,310
So you can try her catch catch well-led error as in this error and then return nil.

110
00:08:51,370 --> 00:09:00,670
And then now we're going to say Try thought document and my auto correct gets a little bit funky here

111
00:09:00,670 --> 00:09:03,390
for some reason every time.

112
00:09:03,400 --> 00:09:18,080
So just follow along closely so thought document is equal to trend's action dot net document.

113
00:09:18,290 --> 00:09:26,780
And then we put in the path to the document which is a fire store and fire store

114
00:09:30,920 --> 00:09:33,660
that going to drop this down to a new line.

115
00:09:33,800 --> 00:09:47,200
That collection of thoughts underscore rough and I need a dot right here collection dot document.

116
00:09:47,840 --> 00:09:55,040
And finally auto complete is back so there we go that document and the document is or the path is self-taught

117
00:09:55,800 --> 00:10:02,570
thought that document Heidi.

118
00:10:02,570 --> 00:10:04,960
All right so a little bit harrowing there for a minute.

119
00:10:04,990 --> 00:10:13,280
I'm not sure why the autocomplete gets all freaky inside of this transaction but just to recap we say

120
00:10:13,280 --> 00:10:20,130
lead thoughts document equal a document snapshot and then we try to get it from transaction dot document.

121
00:10:20,420 --> 00:10:23,800
And then we pass in the path to it.

122
00:10:24,080 --> 00:10:26,880
And then here we're just going to debug.

123
00:10:26,900 --> 00:10:29,450
Print the error if there is one.

124
00:10:29,450 --> 00:10:50,910
When I say Fetch ever and we're going to string interpolate the error dobbed localized description.

125
00:10:51,880 --> 00:10:52,570
All right.

126
00:10:53,140 --> 00:10:55,590
So now we have the thought document.

127
00:10:55,900 --> 00:10:57,120
So what do we need out of it.

128
00:10:57,130 --> 00:10:59,860
We need to know how many likes it currently has.

129
00:10:59,860 --> 00:11:09,370
So we're going to say guard that old gnome likes people and we're going to access the thought document

130
00:11:10,110 --> 00:11:16,880
fact document that data.

131
00:11:16,900 --> 00:11:22,760
So we're going to get the data from it which is a dictionary and we're just going to grab it out of

132
00:11:22,780 --> 00:11:35,800
that dictionary of the number of comments and we're going to cast this as an else returned and I just

133
00:11:35,800 --> 00:11:42,840
realized that I said likes here this should be comments so old mom Common's is equal to thought document

134
00:11:42,850 --> 00:11:47,340
data the number of comments cast it doesn't else return.

135
00:11:47,590 --> 00:11:53,510
So basically all we're doing is what we have here but I need to return now.

136
00:11:53,530 --> 00:11:54,060
There we go.

137
00:11:55,160 --> 00:11:59,210
Basically all we're doing here is we're just drilling down from our thoughts to the document to the

138
00:11:59,210 --> 00:12:08,050
number of comments and extracting that data and then we can just see transaction that update

139
00:12:13,840 --> 00:12:34,040
data and the fields is numb underscore comments and the value is old comments plus one and forward document

140
00:12:34,240 --> 00:12:41,840
the document reference is sulf that thot ref.

141
00:12:42,130 --> 00:12:47,140
All right so now we have taken care of updating the number of comments for the thought that we have

142
00:12:47,140 --> 00:12:49,500
clicked on when we add a comment.

143
00:12:49,780 --> 00:12:52,870
All right next thing we need to do is actually add the comment.

144
00:12:53,080 --> 00:12:53,660
OK.

145
00:12:53,800 --> 00:12:56,430
So let's go and do that now.

146
00:12:56,440 --> 00:13:01,400
In the past we have always used the audio generating document IDs.

147
00:13:01,570 --> 00:13:10,270
However when you use set which you have to use in a transaction you have to explicitly set the ID and

148
00:13:10,270 --> 00:13:17,020
so the workaround for that is to create an empty document first which uses the auto generating ID and

149
00:13:17,020 --> 00:13:18,260
then using that.

150
00:13:18,260 --> 00:13:18,520
All right.

151
00:13:18,550 --> 00:13:29,210
So we're going to say let new comment ref equal self dot fire

152
00:13:31,730 --> 00:13:38,490
store data collection and the collection path is thoughts ref.

153
00:13:38,600 --> 00:13:48,290
Then we drill down one layer deeper to the document and that is at self-taught thought that document

154
00:13:48,290 --> 00:13:56,000
ID and then we go one layer deeper to a new collection called the comments.

155
00:13:56,450 --> 00:13:58,900
And let's go ahead and create a constant for this.

156
00:13:58,940 --> 00:14:01,320
So I'm going to jump back and we're Constans file.

157
00:14:01,610 --> 00:14:10,710
And here with our other references I'm going to say let comments ref equal comments.

158
00:14:10,730 --> 00:14:22,780
Copy this gem back in the comments AVC and set the collection are to comments and then we just say document

159
00:14:25,210 --> 00:14:27,740
that just kind of creates an empty document.

160
00:14:27,840 --> 00:14:35,300
Now when we create our new comment document and we'll we'll pass this comment in the common ref.

161
00:14:35,600 --> 00:14:36,040
OK.

162
00:14:36,150 --> 00:14:44,550
So we're going to see transaction data set data and that's the one we want here for document.

163
00:14:44,720 --> 00:14:49,390
The document here is the new comment ref.

164
00:14:49,760 --> 00:14:59,930
And here the data that we're going to pass in is we're going to do the user name the timestamp and the

165
00:14:59,960 --> 00:15:01,230
comment text.

166
00:15:01,310 --> 00:15:11,530
So I'm going to say comment text and that is of value right here.

167
00:15:11,580 --> 00:15:15,810
Comment text comma.

168
00:15:16,100 --> 00:15:23,120
Then we're going to use the username or the time and this is a concept that we already have and that

169
00:15:23,120 --> 00:15:28,820
is field value that server time stamp.

170
00:15:29,030 --> 00:15:35,100
And then another common comma and the third value that we were adding to our comment is the user name.

171
00:15:35,100 --> 00:15:45,710
So when I say user name and that is also user name and I can hear the self here.

172
00:15:45,740 --> 00:15:50,250
Since we're inside of a closure and then this can turn this into a constant here.

173
00:15:50,270 --> 00:15:55,280
So we're going to copy that as well and come into our Constans file here and here with the rest of the

174
00:15:55,280 --> 00:15:56,070
field contents.

175
00:15:56,090 --> 00:16:04,540
I'll say let comment underscore the extreme equal comment text and then I'll copy this constant here

176
00:16:04,550 --> 00:16:14,200
jump back into comments Visi and replace that save that save our Constans file and back in the comments

177
00:16:14,210 --> 00:16:15,090
visi.

178
00:16:15,410 --> 00:16:16,790
I think that looks good.

179
00:16:17,300 --> 00:16:19,120
So now let's handle if there is an error.

180
00:16:19,160 --> 00:16:28,160
We'll just say if left error is equal to error we are just going to debug printed something to say debug

181
00:16:28,170 --> 00:16:38,860
print and it's going to say transaction failed and print out the error.

182
00:16:39,680 --> 00:16:47,630
Else if it was successful then we're just going to clear out the comment text field so that it's ready

183
00:16:47,630 --> 00:16:48,550
for a new comment.

184
00:16:48,560 --> 00:16:57,760
I going to say self that enter or add comment text that text is equal to an empty string.

185
00:16:58,070 --> 00:17:00,470
All right so that looks good.

186
00:17:00,500 --> 00:17:03,060
Let's go and just review real quick what we did.

187
00:17:03,080 --> 00:17:08,960
All right so when someone clicks on the Add comment button what we do is we run a transaction.

188
00:17:08,960 --> 00:17:13,630
And the reason we use transactions is when you have multiple read and writes all right.

189
00:17:13,640 --> 00:17:18,800
And so with a transaction we have to read it first and so we did that here.

190
00:17:18,980 --> 00:17:25,190
We do have a do catch block where we try to read from the database.

191
00:17:25,220 --> 00:17:32,160
Our thought documents we say get document and then we pass in the path to the thought document.

192
00:17:32,210 --> 00:17:38,870
If something goes wrong then we print it out and then from there from that document that we read we

193
00:17:38,870 --> 00:17:44,060
get to the number of comments data since that's what we want to increment by one.

194
00:17:44,270 --> 00:17:52,490
And as we get that information and then we update that data using the number of comments field and incrementing

195
00:17:52,490 --> 00:17:59,810
it by one and then finally we actually get to the part where we are adding the new document and to do

196
00:17:59,810 --> 00:18:08,180
that we first create a new comment a ref so that we can have an auto generated ID and we create the

197
00:18:08,180 --> 00:18:15,350
new comment here by saying set data and we pass in the data that we want to create and set in our new

198
00:18:15,350 --> 00:18:21,440
document and that is the common text timestamp username and then finally we handle the error if there

199
00:18:21,440 --> 00:18:22,300
is one.

200
00:18:22,310 --> 00:18:23,560
So let's go ahead and test this out.

201
00:18:23,570 --> 00:18:31,700
I'm going to run it all right so I'm going to click on the serious one from Aristotle and I'm going

202
00:18:31,700 --> 00:18:33,140
to write here.

203
00:18:33,140 --> 00:18:34,730
Gonna go ahead and add a comment.

204
00:18:34,880 --> 00:18:39,810
So keep in mind that right now we don't have the capability to.

205
00:18:39,930 --> 00:18:41,870
If we wait if we toggle our keyboard here.

206
00:18:41,990 --> 00:18:46,190
So if you come down a hardware keyboard and if you toggle the software keyboard that's going to cover

207
00:18:46,190 --> 00:18:46,860
it up.

208
00:18:47,240 --> 00:18:52,160
So just keep that keyboard on toggled for now.

209
00:18:52,520 --> 00:18:54,170
We might get to fixing that later.

210
00:18:54,320 --> 00:19:03,600
So when I added comments to say adding first comment when I pressed the button and it cleared.

211
00:19:03,650 --> 00:19:05,130
So let's go ahead and check our.

212
00:19:05,150 --> 00:19:11,280
And we don't get any error statements here so it's good to check our database and find that one.

213
00:19:11,330 --> 00:19:12,860
Let's see here.

214
00:19:12,980 --> 00:19:13,810
Here it is.

215
00:19:14,030 --> 00:19:19,610
So now we can see this is the one that we commented on and it has a subcollection of comments and if

216
00:19:19,610 --> 00:19:26,060
we click on it then we have documents and if we click on that document there it is adding first comment

217
00:19:26,520 --> 00:19:28,490
right so that is pretty cool.

218
00:19:28,490 --> 00:19:35,550
That's going to add a second one simulator and get a second hand added.

219
00:19:36,110 --> 00:19:39,520
And here we can see that it was added right here.

220
00:19:39,520 --> 00:19:40,170
All right.

221
00:19:40,220 --> 00:19:42,150
Very cool.

222
00:19:42,170 --> 00:19:48,050
All right so now we have learned all about transactions and how we can work with multiple read and writes

223
00:19:48,350 --> 00:19:49,310
at once.

224
00:19:49,310 --> 00:19:55,400
And yes now we are adding comments in the next lesson or we are going to work on pulling them down from

225
00:19:55,400 --> 00:19:58,040
this subcollection and displaying them all right.

226
00:19:58,110 --> 00:19:58,710
So I'll see you then.
