1
00:00:06,340 --> 00:00:16,270
What's up guys I'm Jacob Bledsoe at slopes Delcam and we left off getting our run model all all ironed

2
00:00:16,270 --> 00:00:20,640
out and explained some stuff about Rome and what's required.

3
00:00:20,980 --> 00:00:24,380
And now that we have that before we get started this pull up our terminal.

4
00:00:24,400 --> 00:00:30,370
I'm just going to make sure I create a branch for this lesson right.

5
00:00:30,400 --> 00:00:32,090
And that is done.

6
00:00:32,110 --> 00:00:44,610
So when I create data models I like to manipulate everything with that model inside of the this class.

7
00:00:44,620 --> 00:00:52,180
So like when I'm saving to the run object I want to call a function that is inside the run object that

8
00:00:52,180 --> 00:00:53,830
will save everything for me.

9
00:00:54,040 --> 00:00:57,880
And that way you're not you don't have your code all over the place.

10
00:00:57,880 --> 00:01:05,530
Things are nicely contained and like you know if you want to save a run object you just can call the

11
00:01:05,530 --> 00:01:08,950
run class and find exactly what you're looking for.

12
00:01:08,950 --> 00:01:16,710
So when I create objects like we want to save our run object from our current run screen when we end

13
00:01:16,710 --> 00:01:20,280
the run we want to save that object to relm right.

14
00:01:20,340 --> 00:01:27,540
So why don't we make a function and you have to make it static because we only want one instance of

15
00:01:27,540 --> 00:01:30,890
it we don't want it to be able to be overridden or anything like that.

16
00:01:31,080 --> 00:01:41,210
So static func and we will say add run to realm and that is what we're doing.

17
00:01:41,220 --> 00:01:45,590
This lesson is we are saving our run objects to relm OK.

18
00:01:45,870 --> 00:01:52,890
And what do we need to create an object we need our pace which is type and we need our distance which

19
00:01:52,890 --> 00:01:57,410
is a double and we need our duration which is another end.

20
00:01:57,490 --> 00:01:59,270
All right.

21
00:01:59,310 --> 00:02:06,150
And so when the user passes those in everything inside this function will take care of saving it to

22
00:02:06,150 --> 00:02:08,620
relm OK.

23
00:02:08,730 --> 00:02:16,800
But basically what static allows us to do here is we don't have to create a a static instance of the

24
00:02:16,800 --> 00:02:23,950
run object like you we have done and other things we've made like data services and stuff like that.

25
00:02:24,450 --> 00:02:32,610
We'll actually be able to just go to our current run location and if we go to end run we want to add

26
00:02:32,610 --> 00:02:34,640
our relm code here right.

27
00:02:34,650 --> 00:02:43,530
We can just call our run object or run Stut swift file and say add a run to Relm

28
00:02:47,300 --> 00:02:48,950
and that's pretty cool.

29
00:02:48,950 --> 00:02:56,180
One second I wanted to see it save.

30
00:02:56,350 --> 00:03:02,690
So if we add at run to Rome and there we go we can passen the pace great.

31
00:03:02,720 --> 00:03:11,770
We can pass in the distance which what do we we call it run distance run distance and then we can pass

32
00:03:11,820 --> 00:03:15,520
in the counter which is the duration.

33
00:03:16,100 --> 00:03:22,030
So then this is all that's required on our end run to create our relm object and that's really nice.

34
00:03:22,280 --> 00:03:29,600
And so we know we want to create a run object so we call our run class and we can just add our realm

35
00:03:29,600 --> 00:03:30,880
object just like that.

36
00:03:31,190 --> 00:03:35,510
And that's why I like to make my my models this way.

37
00:03:35,690 --> 00:03:42,590
We don't need a static instance of it we make the functions static and everything works from that point

38
00:03:42,650 --> 00:03:43,380
on.

39
00:03:43,790 --> 00:03:51,350
So when we're dealing with a realm you need to create a realm instance for every time you write.

40
00:03:51,380 --> 00:03:52,920
And every time you read.

41
00:03:53,200 --> 00:03:53,860
OK.

42
00:03:54,110 --> 00:03:59,210
So one thing that you need to do is they're going to like the documentation.

43
00:03:59,210 --> 00:04:00,920
Let's just look at this real quick.

44
00:04:02,220 --> 00:04:03,300
When you do a write

45
00:04:06,310 --> 00:04:15,770
says find it they always show just force on rapin with a try but we're not going to force on RAP we're

46
00:04:15,770 --> 00:04:21,380
going to do just the do try with a catch just like you're supposed to do for air catching.

47
00:04:21,560 --> 00:04:27,410
So if an error does happen it's not likely that a realm right air will happen but you want to write

48
00:04:27,410 --> 00:04:28,000
code for it.

49
00:04:28,010 --> 00:04:31,760
Otherwise you're just going to crash instead of recovering from it.

50
00:04:31,760 --> 00:04:32,490
Right.

51
00:04:32,540 --> 00:04:40,880
So we're going to put a do here and at the bottom of do you catch it and then we're going to do is just

52
00:04:40,880 --> 00:04:43,250
do a Debug.

53
00:04:43,250 --> 00:04:49,450
Print and this is just going to be a print statement when we're in debug mode.

54
00:04:49,460 --> 00:04:55,730
So running on the simulator and stuff these actually won't ever get put in your app when you publish

55
00:04:55,730 --> 00:04:56,540
it to the App Store.

56
00:04:56,540 --> 00:05:00,080
That's why I like to use Debug print instead of just a print statement.

57
00:05:00,260 --> 00:05:10,100
And I'm just going to type air air adding run to relm just so we know if we get an air we know where

58
00:05:10,100 --> 00:05:10,640
it occurred.

59
00:05:10,640 --> 00:05:11,540
Right.

60
00:05:11,630 --> 00:05:18,150
So inside this Do statement we need to get a reference of the realm.

61
00:05:18,170 --> 00:05:23,610
So we're going to do let relm equal and then we need to try.

62
00:05:23,750 --> 00:05:29,740
And then we just create an instance of realm just like that and this calling realm like this set calls

63
00:05:29,750 --> 00:05:36,620
the default configuration and all of that stuff so realm out of the box will work just fine for us for

64
00:05:36,620 --> 00:05:38,390
now doing it this way.

65
00:05:38,390 --> 00:05:43,520
And we will run into an issue later where we are going to have to do something a little different than

66
00:05:43,520 --> 00:05:43,910
this.

67
00:05:43,940 --> 00:05:46,150
But for now this is perfect.

68
00:05:46,220 --> 00:05:51,710
And then once you do this you just do try relm with the instance you just made.

69
00:05:51,800 --> 00:05:52,630
Right.

70
00:05:52,820 --> 00:05:59,460
And then you need the opening and closing bracket and then you'd simply say relm dot add.

71
00:06:00,020 --> 00:06:04,910
And then we pass in our object which we haven't created yet but we have everything we need to create

72
00:06:04,910 --> 00:06:05,790
that object.

73
00:06:05,990 --> 00:06:15,440
So why don't we go ahead and say let our run equal and then call this class run and we are looking for

74
00:06:15,440 --> 00:06:26,140
our initialiser right here and we can just simply pass in our pace distance and duration which we passed

75
00:06:26,140 --> 00:06:28,000
in from the other view controller.

76
00:06:28,210 --> 00:06:34,690
So we took all these variables created the run object and now in round out add let's just pass in our

77
00:06:34,690 --> 00:06:42,370
object we just created and get rid of the space hafter realm right.

78
00:06:42,440 --> 00:06:48,920
So with that being done we just added our objects realm it is now being stored.

79
00:06:49,100 --> 00:06:54,220
And then another nice thing to do is try relm don't commit.

80
00:06:54,260 --> 00:06:55,600
Right.

81
00:06:55,730 --> 00:07:02,570
And this kind of just makes sure it doesn't leave with anything open.

82
00:07:02,700 --> 00:07:10,740
You don't need this but when I was I ran into some instances where it is good to confirm that you're

83
00:07:10,740 --> 00:07:12,200
right is committed.

84
00:07:12,600 --> 00:07:17,300
You don't need this but it's it doesn't hurt to have it.

85
00:07:17,400 --> 00:07:23,590
It's a safe safe thing to make sure that the realm right is committed and done when you're finished.

86
00:07:23,940 --> 00:07:26,690
And so right now we are good.

87
00:07:26,760 --> 00:07:31,990
And if we we already are calling our function from our current run VC.

88
00:07:32,070 --> 00:07:43,090
So when we end the run it would it would save one thing that is good because rights to relm are asynchronous.

89
00:07:43,500 --> 00:07:49,200
So we're going to want to take it off the main thread and do like a dispatch.

90
00:07:49,200 --> 00:07:50,970
Q Okay.

91
00:07:52,620 --> 00:07:54,790
Like a cereal dispatch.

92
00:07:54,790 --> 00:07:59,030
Q But we want to make sure we're always calling it on the same thread.

93
00:07:59,250 --> 00:08:06,020
So if we go to our utilities folder let's create a new file and a swift files.

94
00:08:06,030 --> 00:08:09,070
Good and let's call this Constance

95
00:08:12,070 --> 00:08:17,830
and we're just going to create some constants here that we can use globally everywhere.

96
00:08:17,830 --> 00:08:23,780
So this is going to be let and we'll call this realm underscore.

97
00:08:24,300 --> 00:08:33,480
Q And we're are going to set this equal to a dispatch queue Q And then we can give it a key.

98
00:08:33,520 --> 00:08:38,060
So it always uses the same dispatch queue and we'll call this our realm.

99
00:08:38,060 --> 00:08:44,620
Q Nice and easy and see that I just made this a global variable so I kept everything all caps so it's

100
00:08:44,620 --> 00:08:46,600
very obvious that's what it is.

101
00:08:46,900 --> 00:08:54,570
And if we go to our run here all we do to call that would be Rome.

102
00:08:54,610 --> 00:09:03,490
Q And now are our dispatch queue will queue up all of our asynchronous rights if that happens like we

103
00:09:03,490 --> 00:09:04,740
called a bunch in a row.

104
00:09:04,900 --> 00:09:07,920
They could stack up but at least we're taking them off our main thread.

105
00:09:08,230 --> 00:09:14,910
And this is allowed because it's always going to the same thread now.

106
00:09:14,950 --> 00:09:21,760
And so if we ran this let's just make sure our current run calls yes our current run is coin.

107
00:09:21,790 --> 00:09:24,000
Add to around and we're passing it in.

108
00:09:24,220 --> 00:09:31,600
So if we run this app let's just get I don't want the 7 plus it's too big takes too much time to get

109
00:09:31,600 --> 00:09:32,280
it going.

110
00:09:37,640 --> 00:09:42,030
Or so our apps loaded up were already on a run.

111
00:09:42,030 --> 00:09:49,080
You see it working with say start run and duration starts counting pace starts calculating and distance

112
00:09:49,170 --> 00:09:51,250
starts increasing.

113
00:09:51,600 --> 00:09:54,120
So we have a run here right now.

114
00:09:54,120 --> 00:10:02,230
If I end this run it should add our data to relm and we didn't get any crashes.

115
00:10:02,350 --> 00:10:06,880
So that's a good sign that means it successfully added our object.

116
00:10:06,880 --> 00:10:09,260
So why don't we just start around one more time.

117
00:10:09,280 --> 00:10:16,290
So we have a couple pieces of data when we start reading it.

118
00:10:16,400 --> 00:10:16,940
All right.

119
00:10:16,940 --> 00:10:22,400
So let's just I don't remember what we took our time to last time or distance let's just make sure it's

120
00:10:22,400 --> 00:10:23,660
different.

121
00:10:23,660 --> 00:10:25,730
All right let's go ahead and end this run.

122
00:10:25,730 --> 00:10:30,170
So now we have two runs to run objects saved to our realm database.

123
00:10:30,440 --> 00:10:31,800
So that's really cool.

124
00:10:31,820 --> 00:10:34,020
And we don't have any crashes.

125
00:10:34,220 --> 00:10:40,370
So now we have to start getting them displaying in our table view log right.

126
00:10:40,370 --> 00:10:48,230
So to do that let's go back to our run model and in our run model we're going to have to make a function

127
00:10:48,230 --> 00:10:50,050
for retrieving our data.

128
00:10:50,090 --> 00:10:50,840
OK.

129
00:10:51,410 --> 00:10:54,940
So we're going to do the same thing we're going to make a static function.

130
00:10:55,310 --> 00:11:01,180
So go ahead and type static func and we'll call this get all runs.

131
00:11:02,480 --> 00:11:04,670
And this needs to return

132
00:11:09,030 --> 00:11:20,930
a collection called results and what results are is pretty much like a dictionary were it's unordered.

133
00:11:21,210 --> 00:11:30,150
So every time you would get all your runs you can't guarantee the order but you can like say you want

134
00:11:30,180 --> 00:11:36,360
object 0 like index 0 like an array and pull that out but it's not always guaranteed to be the same

135
00:11:36,420 --> 00:11:37,270
object.

136
00:11:37,290 --> 00:11:45,630
So results are not ordered So when you do get your results you need to sort them and do all that cool

137
00:11:45,630 --> 00:11:47,470
stuff but we'll get there in a second.

138
00:11:47,700 --> 00:11:52,980
And we want a we want a collection of results with type run inside.

139
00:11:52,980 --> 00:11:59,790
So our object run and this is going to be an optional because we're calling the realm database.

140
00:11:59,790 --> 00:12:01,650
We don't even know if there is data there.

141
00:12:01,710 --> 00:12:02,010
Right.

142
00:12:02,010 --> 00:12:06,850
So it's an optional value and then opening and closing brackets is perfect.

143
00:12:07,050 --> 00:12:12,370
And let me just do some spaces here so my code is more in the center for you guys.

144
00:12:12,770 --> 00:12:20,420
And right when we are doing when we're retrieving our realm object we are going to do a do catch again.

145
00:12:20,430 --> 00:12:26,130
So if it does fail when we create our instance to realm at least we're catching the error in our app

146
00:12:26,130 --> 00:12:27,190
doesn't crash.

147
00:12:27,240 --> 00:12:30,030
We might not have data but at least her app doesn't crash.

148
00:12:30,060 --> 00:12:40,260
So do it do whoops do it do opening brackets closing brackets and then catch opening and closing brackets

149
00:12:40,470 --> 00:12:41,840
and end the catch.

150
00:12:41,880 --> 00:12:45,610
We're just going to return nil because it's an optional right.

151
00:12:45,660 --> 00:12:49,920
So if nothing is there we'll just return nothing.

152
00:12:49,920 --> 00:12:57,540
So if it errors out polling our run our run collection from the database will just return nil which

153
00:12:57,540 --> 00:12:58,280
is fine.

154
00:12:59,870 --> 00:13:02,510
So now we need to create an instance to Rome.

155
00:13:02,510 --> 00:13:11,030
So we need to do let relm equal try realm just like above we need an instance to just our default realm

156
00:13:11,060 --> 00:13:17,380
configuration and then we need to create a variable for the runs.

157
00:13:17,390 --> 00:13:18,090
OK.

158
00:13:19,710 --> 00:13:32,720
And so we can simply just do var runs equals relm dot objects of type runs so we want our object.

159
00:13:32,760 --> 00:13:34,010
Our model run.

160
00:13:34,320 --> 00:13:42,090
And it just needs to be self so we're calling it's we're calling our run object.

161
00:13:42,140 --> 00:13:42,780
OK.

162
00:13:44,020 --> 00:13:51,520
And so this is as much as you have to do to actually get a value we could just return this so we could

163
00:13:51,520 --> 00:13:56,480
just do the return runs.

164
00:13:56,530 --> 00:13:59,210
There we go and we could get data now.

165
00:14:02,910 --> 00:14:03,920
Why don't we do that.

166
00:14:03,960 --> 00:14:09,340
And then we'll get into making sure we sort it by the date and all that stuff.

167
00:14:09,630 --> 00:14:17,010
So in our location in our begin run VC just in view did load.

168
00:14:17,040 --> 00:14:25,620
Let's just do a simple print statement and it'll be like here are my runs colon and then backslash opening

169
00:14:25,620 --> 00:14:27,160
and closing parentheses.

170
00:14:27,240 --> 00:14:35,820
And let's just call our run swift file and get all runs and this should just print out two objects for

171
00:14:35,820 --> 00:14:38,820
us down here in our console.

172
00:14:38,820 --> 00:14:40,520
So let's go ahead and run this

173
00:14:44,270 --> 00:14:50,880
and this is yelling at us because it's an optional and that wants a default value but that's fine for

174
00:14:50,880 --> 00:14:51,650
now.

175
00:14:52,050 --> 00:14:53,430
And look at that.

176
00:14:53,640 --> 00:15:02,280
I'm going to just end this here and we have our two run objects with are automatically created you know

177
00:15:02,280 --> 00:15:08,600
our our randomly generated IDs and then we have our N.S. date of when it was created.

178
00:15:08,940 --> 00:15:15,410
And we also have our pace here in seconds our duration in seconds in our distance.

179
00:15:15,450 --> 00:15:19,440
So this is really cool our distance in meters right.

180
00:15:19,560 --> 00:15:21,650
So we have all of our data here.

181
00:15:21,750 --> 00:15:26,790
But now if we run this again I just want to see the duration.

182
00:15:26,790 --> 00:15:30,570
So the first one is 15 right now for duration.

183
00:15:30,570 --> 00:15:35,160
Let's just run this again and see if 15 is still in the top it's not guaranteed.

184
00:15:35,160 --> 00:15:42,060
But you also don't it could just happen again because we only have 2 objects and it is in the top but

185
00:15:42,110 --> 00:15:48,520
it's just like dictionaries the order is not guaranteed but that's really cool that we have data there.

186
00:15:48,570 --> 00:15:50,390
And you're seeing it.

187
00:15:50,390 --> 00:15:54,450
So let's go back to our run here and a really cool thing.

188
00:15:55,790 --> 00:16:03,710
In our Since we've made date an index property we can simply just do this for sorting we can say we

189
00:16:03,710 --> 00:16:07,610
want our runs variable to equal runs.

190
00:16:07,640 --> 00:16:14,900
So we're calling this data still and we're just going to say daat sorted and we want it by key path

191
00:16:15,920 --> 00:16:22,370
and we just called date here is spelled the same exact way as our variable.

192
00:16:22,370 --> 00:16:23,020
All right.

193
00:16:23,240 --> 00:16:26,170
And then we want ascending to be false.

194
00:16:26,210 --> 00:16:31,290
If we made ascending true it would be the very first run would come first.

195
00:16:31,310 --> 00:16:34,560
But in this case we won our last run to come first right.

196
00:16:34,760 --> 00:16:42,260
So we always want the newest date to be on top and that is as much as we have to do to sort our runs

197
00:16:42,260 --> 00:16:43,080
in order.

198
00:16:43,100 --> 00:16:49,130
So now if we run this again and this prince it's probably going to be in the same order I didn't really

199
00:16:49,130 --> 00:16:53,480
pay attention to the time stamp but we'll see.

200
00:17:00,090 --> 00:17:02,170
Oh I removed the print statement.

201
00:17:02,300 --> 00:17:07,390
That's I'm just going to undo and.

202
00:17:07,490 --> 00:17:07,770
All right.

203
00:17:07,790 --> 00:17:11,750
Now let's run it again and we'll see our runs and nothing.

204
00:17:11,750 --> 00:17:16,400
It might not be any different but it will show it'll always return that same order.

205
00:17:16,400 --> 00:17:19,930
Now oh and there we go.

206
00:17:19,930 --> 00:17:25,990
So the duration for 16 was actually sooner than this one.

207
00:17:25,990 --> 00:17:32,170
And so now this order will always return this way because we're sorting it by the time stamp which is

208
00:17:32,170 --> 00:17:33,100
really cool.

209
00:17:34,090 --> 00:17:36,700
And it didn't take any effort to sort that.

210
00:17:36,700 --> 00:17:37,210
So

211
00:17:39,910 --> 00:17:45,670
right there is like one of the huge reasons for using realm aside from like off line data persistence

212
00:17:45,670 --> 00:17:47,350
and all that cool stuff.

213
00:17:47,350 --> 00:17:51,560
So that is how you would retrieve your runs which is nice and easy.

214
00:17:51,580 --> 00:17:57,160
We will call this video done and in the next video we are going to be grabbing all of our runs data

215
00:17:57,580 --> 00:18:02,610
and getting it in our collection view in our in our run log.

216
00:18:02,620 --> 00:18:06,280
Let me just find our run log here.

217
00:18:06,280 --> 00:18:11,040
So we'll load everything on the table view straight from our role model we won't store it anywhere else

218
00:18:11,050 --> 00:18:12,760
we're just grabbing straight from Rome.

219
00:18:12,790 --> 00:18:13,900
So that's really cool.

220
00:18:13,900 --> 00:18:15,370
So before I get off.

221
00:18:15,880 --> 00:18:29,200
Let's go ahead and do a get ad period a commit Dasch am and we did let's see ad and I should say right

222
00:18:29,290 --> 00:18:41,720
and fetch methods for around for a run object for relm or for run model done.

223
00:18:41,990 --> 00:18:42,630
OK.

224
00:18:42,640 --> 00:18:45,590
So there we go and I will see you in the next lesson guys.
