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