1
00:00:08,120 --> 00:00:13,490
Hey everybody this is Caleb with Dev soapstar calm and in this fantastic video we're going to write

2
00:00:13,490 --> 00:00:17,930
a function to remove objects from our core data persistent store.

3
00:00:17,930 --> 00:00:19,180
We're going to remove them forever.

4
00:00:19,210 --> 00:00:20,290
Going to get rid of them.

5
00:00:20,600 --> 00:00:25,340
And we're also going to add the ability to delete them from our table view cell.

6
00:00:25,340 --> 00:00:27,540
We're going to be able to swipe tap delete.

7
00:00:27,560 --> 00:00:32,360
And we're going to be able to remove it and obliterate it forever as well as reload our table view and

8
00:00:32,360 --> 00:00:34,640
show all of the changes right away.

9
00:00:34,640 --> 00:00:35,570
It's so cool.

10
00:00:35,570 --> 00:00:41,360
So let's begin by pulling open that X code project and going into goals.

11
00:00:41,360 --> 00:00:48,620
VC OK now we're going to go down to our extension and we're going to write a function to remove objects

12
00:00:48,620 --> 00:00:50,850
from a managed context.

13
00:00:50,850 --> 00:00:51,380
OK.

14
00:00:51,650 --> 00:00:59,420
Now to do this go ahead and type phunk remove let's do remove goal K and we're going to remove a goal

15
00:00:59,420 --> 00:01:01,100
at a certain index path right.

16
00:01:01,100 --> 00:01:07,280
We want to pull it out from our array of managed objects so we're going to go ahead and say an index

17
00:01:07,670 --> 00:01:14,300
path and we're going to pass an internal parameter here of index path which is a type index path lots

18
00:01:14,300 --> 00:01:15,380
of index path there.

19
00:01:15,710 --> 00:01:19,080
And we're going to go ahead and use that to remove our object.

20
00:01:19,160 --> 00:01:19,790
OK.

21
00:01:20,120 --> 00:01:23,790
Now we need to get another managed context here.

22
00:01:23,930 --> 00:01:26,740
So we're going to create a reference to the managed context.

23
00:01:26,930 --> 00:01:28,210
So let's do that by typing.

24
00:01:28,230 --> 00:01:35,180
Guard let managed context equals app delegate that persistent container view context.

25
00:01:35,390 --> 00:01:39,050
And if we don't have that we're going to just return just like below.

26
00:01:39,050 --> 00:01:43,150
We need just the reference to our managed context inside this function.

27
00:01:43,160 --> 00:01:47,750
Next what we're going to do is we're going to call managed context delete.

28
00:01:47,860 --> 00:01:48,470
OK.

29
00:01:48,890 --> 00:01:51,460
Now we need to delete a certain object.

30
00:01:51,470 --> 00:01:59,180
And as you know this array of goals is of type goal which if you go to the declaration of goal let's

31
00:01:59,180 --> 00:02:00,080
go find it.

32
00:02:00,080 --> 00:02:04,190
We can see that it inherits from an S. managed object from core data.

33
00:02:04,190 --> 00:02:10,820
So if we go back down we need to get a certain N-S managed object and to do that we're going to go into

34
00:02:10,820 --> 00:02:15,640
the goals array and we're going to pull out the item at the index path dot row.

35
00:02:15,710 --> 00:02:16,520
We ask.

36
00:02:16,610 --> 00:02:17,120
OK.

37
00:02:17,360 --> 00:02:20,360
We're going to call this from our table view.

38
00:02:20,360 --> 00:02:25,460
When we tap on a certain cell we're going to pass the index view of that cell which is going to correspond

39
00:02:25,460 --> 00:02:29,450
exactly with the goal from the goals array because that's how the data is shown in the table view.

40
00:02:29,450 --> 00:02:30,040
Right.

41
00:02:30,350 --> 00:02:33,030
And then we're going to call managed context not delete.

42
00:02:33,080 --> 00:02:37,160
That's a function that's going to delete the goal at that and X path.

43
00:02:37,270 --> 00:02:37,910
OK.

44
00:02:38,150 --> 00:02:41,720
Now that is not enough though to actually delete what we need.

45
00:02:41,720 --> 00:02:44,830
We need to call managed contexts save.

46
00:02:44,930 --> 00:02:50,520
Do you remember in set when we saved the function save we needed to settle this data.

47
00:02:50,540 --> 00:02:55,580
But then in order for it to actually pass in we need to call save on the managed context.

48
00:02:55,580 --> 00:03:00,710
We're going to do the same thing in remove We need to tell it we need to tell the manager contex that

49
00:03:00,710 --> 00:03:01,940
it needs to update itself.

50
00:03:01,940 --> 00:03:10,000
So we're going to use a do and catch block in order to do this like you may have been expecting and

51
00:03:10,030 --> 00:03:14,080
all we're going to do is just say Try managed context save.

52
00:03:14,480 --> 00:03:18,160
You probably noticed that said you probably noticed that it said throws.

53
00:03:18,200 --> 00:03:21,380
And so we're going to go ahead and debug print.

54
00:03:21,710 --> 00:03:26,460
And we're going to say could not remove because that's what we're doing here.

55
00:03:26,810 --> 00:03:33,110
And we're going to take the error from the dot save function and we're going to call error that localize

56
00:03:33,110 --> 00:03:33,720
description.

57
00:03:33,750 --> 00:03:36,760
That'll give us a little bit more information if there's a problem.

58
00:03:36,800 --> 00:03:43,730
So let's say that we do successfully remove an object we're going to go ahead and just print successfully

59
00:03:44,470 --> 00:03:45,390
removed.

60
00:03:45,550 --> 00:03:50,330
Gul All right it looks good.

61
00:03:50,330 --> 00:03:55,700
So you know what guys this is all we need to do to remove something we need to find the item at the

62
00:03:55,700 --> 00:03:57,690
certain index path we want to remove.

63
00:03:57,740 --> 00:04:02,480
We're going to remove it from our managed context and we're going to save that managed context to update

64
00:04:02,480 --> 00:04:03,410
everything.

65
00:04:03,500 --> 00:04:05,280
That's all we've got to do to remove a goal.

66
00:04:05,300 --> 00:04:06,770
It's that easy.

67
00:04:06,800 --> 00:04:12,250
Next we need to enable our table view to be able to swipe and edit cells.

68
00:04:12,320 --> 00:04:16,380
So there are a couple of different table view delegate methods we need to call.

69
00:04:16,520 --> 00:04:20,330
We need to set a property called can edit row an index path.

70
00:04:20,330 --> 00:04:24,160
So go ahead and call that can edit row at index path.

71
00:04:24,200 --> 00:04:29,780
Believe it or not all we need to do to enable editing is call or is return true.

72
00:04:30,170 --> 00:04:31,480
Now we can edit our table view.

73
00:04:31,490 --> 00:04:32,960
It's that easy.

74
00:04:32,960 --> 00:04:36,640
Next what we're gonna do is we're going to go ahead and set the editing style.

75
00:04:36,770 --> 00:04:43,700
So we're going to go ahead and call editing style Foro an index path and we're going to return a table

76
00:04:43,700 --> 00:04:45,790
view editing style.

77
00:04:46,010 --> 00:04:50,620
And if you push if you put a period you can see there are a few different things.

78
00:04:50,810 --> 00:04:54,310
There is insert delete and there is none.

79
00:04:54,320 --> 00:04:58,500
Now insert is going to add a plus a green circle and closing with a little plus sign.

80
00:04:58,580 --> 00:05:00,740
Delete makes a red circle with a dash.

81
00:05:00,740 --> 00:05:04,910
We don't want either of those to show up on our cells because we have kind of a custom configuration

82
00:05:04,910 --> 00:05:05,410
here.

83
00:05:05,600 --> 00:05:07,280
So we're going to just press none.

84
00:05:07,370 --> 00:05:09,300
That way it doesn't show up anything special.

85
00:05:09,470 --> 00:05:13,220
And so we can actually get rid of the table view cell editing style thing.

86
00:05:13,340 --> 00:05:16,200
So our Eddings style has been saved.

87
00:05:16,220 --> 00:05:20,680
Now we need to actually create the editing actions so we can edit it.

88
00:05:20,690 --> 00:05:27,280
Now let's create the actions so let's go ahead and type edit actions forro at index path.

89
00:05:27,380 --> 00:05:28,410
Very very cool.

90
00:05:28,430 --> 00:05:30,470
So we're going to create two actions.

91
00:05:30,470 --> 00:05:36,780
One is going to delete and one is going to add progress in this video we're only focusing on deleting.

92
00:05:36,800 --> 00:05:39,830
So let's go ahead and create our delete action.

93
00:05:39,830 --> 00:05:41,390
Go ahead and type let delete.

94
00:05:41,390 --> 00:05:44,000
Action equals UI.

95
00:05:44,000 --> 00:05:47,270
Table View row action.

96
00:05:47,300 --> 00:05:49,010
Whoops row action.

97
00:05:49,040 --> 00:05:54,620
And what we're going to do is put a parentheses there so that we can instantiate it properly first with

98
00:05:54,620 --> 00:05:56,850
a style then with a title and last.

99
00:05:56,850 --> 00:06:00,550
There's a handler at the end where we actually set what the action does.

100
00:06:00,560 --> 00:06:07,400
So we're going to set the style to be destructive because destructive is what you use when data is going

101
00:06:07,400 --> 00:06:08,630
to be destroyed.

102
00:06:08,690 --> 00:06:11,440
The title is going to say delete.

103
00:06:11,930 --> 00:06:14,800
OK we're going to use all caps just so it looks extra cool.

104
00:06:14,870 --> 00:06:17,990
Deleting should be a very final thing.

105
00:06:18,320 --> 00:06:20,680
And press enter on the handler.

106
00:06:20,840 --> 00:06:21,800
And I want you to notice.

107
00:06:21,830 --> 00:06:26,930
It's going to be returning a table view row action and an index path to us.

108
00:06:27,020 --> 00:06:27,650
OK.

109
00:06:27,650 --> 00:06:29,470
We're going to need the index path.

110
00:06:29,470 --> 00:06:31,910
We won't need the row action but we still need to give it a name.

111
00:06:31,910 --> 00:06:38,910
So go ahead and call RHO action WIPs action and the index path just type index path.

112
00:06:38,930 --> 00:06:43,190
So this is where we're going to actually do the stuff when we push the delete button.

113
00:06:43,190 --> 00:06:51,380
So in order to actually remove something we need to call self remove goal that function we just wrote

114
00:06:51,790 --> 00:06:54,500
Where are we going to get the index path from here.

115
00:06:54,500 --> 00:06:56,940
It's returned to us super easy.

116
00:06:56,960 --> 00:07:01,540
So go ahead and type index path and we have now remove that goal and saved it.

117
00:07:01,790 --> 00:07:08,090
Next what we need to do is we need to fetch all the data from our core data model because we've just

118
00:07:08,090 --> 00:07:08,780
removed it.

119
00:07:08,780 --> 00:07:12,320
It's changed now we need to fetch it all and we need to update our table view.

120
00:07:12,320 --> 00:07:15,530
So go ahead and call self fetch.

121
00:07:15,650 --> 00:07:19,070
But you know what we don't want to go through all of this completion junk again.

122
00:07:19,070 --> 00:07:23,210
So let's go ahead and let's find where we called it earlier here.

123
00:07:23,480 --> 00:07:27,890
And we're going to actually put this into its own function so we can call it from anywhere in this in

124
00:07:27,890 --> 00:07:28,520
this class.

125
00:07:28,520 --> 00:07:34,010
So cut this and below we're going to call phunk fetch.

126
00:07:34,130 --> 00:07:45,440
Fetch core data objects and pass that in there so we can now call fecche core data objects and that

127
00:07:45,440 --> 00:07:49,340
will do the same thing that we need to as when we want to call it here.

128
00:07:49,340 --> 00:07:55,670
So instead of calling the function from our extension we can call fecche core data objects and it does

129
00:07:55,670 --> 00:07:56,630
the same exact thing.

130
00:07:56,630 --> 00:07:59,680
We don't have to call on this big block of code.

131
00:07:59,690 --> 00:08:01,380
This one here we don't have to call it twice.

132
00:08:01,400 --> 00:08:05,030
We can just call it once with that function or we can call the same name twice.

133
00:08:05,030 --> 00:08:06,220
We can write less code.

134
00:08:06,320 --> 00:08:12,680
So now that we fetched all of the objects all the data is updated we can go ahead and call table view

135
00:08:13,310 --> 00:08:16,400
delete rows at index path.

136
00:08:16,490 --> 00:08:21,390
And what this is going to do is it's going to remove a certain row that we have deleted and then animate

137
00:08:21,440 --> 00:08:23,180
closing it's really cool.

138
00:08:23,180 --> 00:08:26,450
So it's asking for an array of index path.

139
00:08:26,630 --> 00:08:30,750
So we're going to give it that an array of index path from the returned index path.

140
00:08:30,890 --> 00:08:32,740
We're going to go ahead and give it an animation.

141
00:08:32,750 --> 00:08:33,340
And you know what.

142
00:08:33,350 --> 00:08:38,150
There are quite a few of them but we're just going to use Automattic K that's just a nice little fade

143
00:08:38,150 --> 00:08:42,280
up it pulls all the ones below it up and kind of animates it down.

144
00:08:42,280 --> 00:08:43,640
It's very cool.

145
00:08:43,640 --> 00:08:48,330
So that is our delete action that's what we want to happen when we push delete.

146
00:08:48,330 --> 00:08:54,420
But we have not yet returned it to our table view yet we just have created it.

147
00:08:54,480 --> 00:08:58,830
But first before we do that we need to set a background color because you can set the color of these

148
00:08:58,830 --> 00:09:00,660
buttons to be whatever you want.

149
00:09:00,690 --> 00:09:08,790
But I found that it's easy if you just type delete action whoops delete action background color and

150
00:09:08,790 --> 00:09:13,350
we're just going to use a color literal so tap on the color little double click it and we're going to

151
00:09:13,350 --> 00:09:15,330
set it to be a nice red color.

152
00:09:15,330 --> 00:09:17,080
Let's go ahead and let's do.

153
00:09:17,120 --> 00:09:17,790
Ooh yeah.

154
00:09:17,920 --> 00:09:19,080
You know that's nice.

155
00:09:19,080 --> 00:09:20,130
So select mirror Shino.

156
00:09:20,130 --> 00:09:21,790
That just means red I think.

157
00:09:22,110 --> 00:09:28,660
And now that we have set the background color we can properly return our delete action.

158
00:09:28,820 --> 00:09:32,820
OK but if I do that watch what happens it yells at me it's expecting an array.

159
00:09:32,820 --> 00:09:37,200
So even though there's only one we need to put it inside of an array we're going to add a second one

160
00:09:37,200 --> 00:09:38,350
later so it's OK.

161
00:09:38,640 --> 00:09:42,360
So go ahead and build and run this well for now I'm just building it.

162
00:09:42,570 --> 00:09:44,470
But now build and run.

163
00:09:44,490 --> 00:09:50,280
And let's go check it out in our applet see if we can properly delete an item from Chordata calling

164
00:09:50,280 --> 00:09:56,010
move goal removing the item at the index path fetching all of the objects after the data is changed

165
00:09:56,040 --> 00:09:57,640
and reloading the rows.

166
00:09:57,660 --> 00:10:01,470
So if we swipe we should be able to see our delete button.

167
00:10:01,470 --> 00:10:02,690
Very cool.

168
00:10:02,760 --> 00:10:04,410
Let's try to delete this one if I press it.

169
00:10:04,410 --> 00:10:06,100
Watch what happens.

170
00:10:06,300 --> 00:10:10,980
It deletes and I don't know if you saw that but it said successfully fetch data successfully removed

171
00:10:10,980 --> 00:10:12,360
goals successfully fetched data.

172
00:10:12,360 --> 00:10:13,780
So this is the initial loading.

173
00:10:14,070 --> 00:10:19,450
That's when I removed the goal then it fetched the new data and deleted the row it showed the animation.

174
00:10:19,470 --> 00:10:24,030
Now you don't only have to swipe and press the button you can actually swipe all the way through and

175
00:10:24,030 --> 00:10:24,660
it will delete it.

176
00:10:24,660 --> 00:10:30,240
It's so cool that's just kind of an added thing that's in there for us so we can now successfully remove

177
00:10:30,240 --> 00:10:35,990
any goals that we want and show our table view when it's done because remember we have conditional code.

178
00:10:36,000 --> 00:10:42,900
If it's less then or if it's less than 1 if it's 0 it's going to hide our table view just like that.

179
00:10:43,050 --> 00:10:44,490
Super super cool guys.

180
00:10:44,490 --> 00:10:45,670
This is amazing.

181
00:10:45,690 --> 00:10:51,500
The only thing that we need to do next is to add a new action here for adding progress.

182
00:10:51,690 --> 00:10:58,200
And at the very end when we meet our progress goal we need to set a UI view to fade in saying goal complete

183
00:10:58,620 --> 00:11:03,180
We're going to do that in the next video and we will call this app finished amazing work.

184
00:11:03,180 --> 00:11:06,210
Let's move on to that last video and let's get it started.
