1
00:00:05,370 --> 00:00:06,180
What's up guys.

2
00:00:06,210 --> 00:00:09,090
Jacob Bledsoe with Dev slopes dot com.

3
00:00:09,480 --> 00:00:17,670
So in the last lesson we have everything completed as far as we need to have all the run data from one

4
00:00:17,670 --> 00:00:18,970
run at a time.

5
00:00:19,050 --> 00:00:26,760
But our problem is let's just run this real quick.

6
00:00:26,760 --> 00:00:31,950
Our problem is when we start our run we have data right.

7
00:00:31,960 --> 00:00:34,420
We have our pace and we have distance.

8
00:00:34,830 --> 00:00:38,760
But when we end our run we don't have any data.

9
00:00:38,770 --> 00:00:47,280
So we have to get our get all of our data saved and showing up so now it's time to get ready for realm

10
00:00:47,290 --> 00:00:49,230
right.

11
00:00:49,960 --> 00:00:54,020
So to begin we have to install the realm pod.

12
00:00:54,040 --> 00:00:54,930
OK.

13
00:00:54,970 --> 00:00:59,260
So we can actually we need to close out of our project for this.

14
00:00:59,260 --> 00:01:07,660
So close this and let's see you got some extra stuff and pull up your terminal and Wellar at this I'm

15
00:01:07,660 --> 00:01:11,320
going to go ahead and create this lesson's branch.

16
00:01:11,500 --> 00:01:13,200
Less than 12.

17
00:01:13,740 --> 00:01:14,470
OK.

18
00:01:14,650 --> 00:01:16,910
And now we need to do a pod in it.

19
00:01:16,930 --> 00:01:26,450
And that basically will install our pod or it will create our pod file and it will also create our workspace

20
00:01:26,480 --> 00:01:28,130
instead of our project file.

21
00:01:28,250 --> 00:01:32,880
So go do Padan it right.

22
00:01:32,890 --> 00:01:37,710
And that is done now let's go ahead and just pull up our project.

23
00:01:37,790 --> 00:01:44,420
So it's in my documents of slopes courses and treads.

24
00:01:44,420 --> 00:01:48,610
And now as you can see it created our pod file.

25
00:01:48,620 --> 00:01:54,800
I'm sorry I also said it would create our workspace but we have to add everything we need we need to

26
00:01:54,830 --> 00:01:56,420
add our pod to our pod file.

27
00:01:56,420 --> 00:02:02,360
Then when we install that it will create our our workspace that we would now work from.

28
00:02:02,360 --> 00:02:07,660
So if we go ahead and say open this with a text editor Here's our APOD file.

29
00:02:07,790 --> 00:02:09,920
Everything is perfect here.

30
00:02:10,250 --> 00:02:14,820
We just need to now put our pod in for realm.

31
00:02:15,140 --> 00:02:19,710
And so the best thing to do is let's go to the realm docs are right.

32
00:02:19,730 --> 00:02:28,010
So over here if you go to a realm I go forward slash docs you'll get to this nice screen and we were

33
00:02:28,010 --> 00:02:35,500
interested in the get started and click on installation and you can read through all this good stuff.

34
00:02:35,500 --> 00:02:40,040
We were interested in swift right now if you want to set up relm for other things.

35
00:02:40,060 --> 00:02:42,030
All the docs are here for you.

36
00:02:42,460 --> 00:02:49,360
So this kind of jumps into an introduction with everything let's just click over here on the left side

37
00:02:49,360 --> 00:02:56,070
get started and we're interested in installation for cocoa pods.

38
00:02:56,860 --> 00:02:57,330
Right.

39
00:02:57,340 --> 00:03:02,980
And all we have to do is use frameworks which by default.

40
00:03:03,010 --> 00:03:04,320
We already are.

41
00:03:04,390 --> 00:03:10,840
And then we need pod realm swift so I'm just going to copy and paste this POD Rhome swift and let's

42
00:03:10,840 --> 00:03:18,820
go back to our pod file and right underneath use frameworks we can fix all this.

43
00:03:18,970 --> 00:03:26,230
Once our projects created I'm just going to paste this here saved this text file and go ahead and close

44
00:03:26,230 --> 00:03:26,950
out.

45
00:03:27,310 --> 00:03:34,140
And now from our terminal when you're inside our project now we just have to do a pod install and there

46
00:03:34,140 --> 00:03:34,630
we go.

47
00:03:34,620 --> 00:03:39,390
Installed relm 2.8 too great.

48
00:03:39,510 --> 00:03:46,890
And it's telling us that we should specify a version I think in our pod file it does 9 automatically

49
00:03:46,890 --> 00:03:48,050
which is fine.

50
00:03:48,450 --> 00:03:52,730
And there goes it created a pod file clock and our workspace code.

51
00:03:52,730 --> 00:03:58,220
Now you always want to open the X-C workspace not the X code project anymore.

52
00:03:58,350 --> 00:04:06,840
So go ahead and open the workspace code and it's our same project now it's a directory for POD's.

53
00:04:07,050 --> 00:04:13,140
And if you just pull this down and go into the pod file you'll see that our pod of realms with does

54
00:04:13,140 --> 00:04:17,840
install and I'm just going to tab that over and that's all we need to do.

55
00:04:18,300 --> 00:04:24,270
And what it was actually yelling at us for it wants us to specify what was we're going to do if we just

56
00:04:24,270 --> 00:04:28,030
get rid of that hash tag we'll say down to nine point OK.

57
00:04:29,570 --> 00:04:30,970
And that should be good.

58
00:04:31,010 --> 00:04:39,610
Let's go ahead and now for that to actually take take or start working we should do a pot install again.

59
00:04:39,620 --> 00:04:42,600
But we shouldn't get that warning.

60
00:04:42,630 --> 00:04:45,540
There we go and that warning's gone.

61
00:04:45,970 --> 00:04:46,710
OK great.

62
00:04:46,710 --> 00:04:56,350
Now if we just open up our project again expand the treads folder and let's see in our model folder

63
00:04:56,350 --> 00:04:58,520
we need to create our run model.

64
00:04:58,540 --> 00:05:06,820
And if we go to our current run VC we have our duration our pace and our distance that we need to keep

65
00:05:06,820 --> 00:05:08,060
track of right.

66
00:05:08,190 --> 00:05:15,180
And I'm pretty sure that's all we're going to need in our in our model right unless you guys can think

67
00:05:15,180 --> 00:05:16,790
of anything else.

68
00:05:17,720 --> 00:05:24,050
Which we might be running across shortly but let's go ahead and make our our model so go ahead and right

69
00:05:24,050 --> 00:05:29,690
click model folder and say new file and we want ios in a swift file.

70
00:05:29,690 --> 00:05:30,620
OK.

71
00:05:31,220 --> 00:05:44,990
And we are going to call this run and hit create and now we have an empty and empty swift file and first

72
00:05:44,990 --> 00:05:52,100
we need to import realm or relm Swift right.

73
00:05:52,540 --> 00:06:03,390
And now we need to create our object which is going to be class run and it needs to inherit from object.

74
00:06:04,230 --> 00:06:05,810
And let's go ahead and build this.

75
00:06:05,830 --> 00:06:07,840
So it finds the realm swift

76
00:06:17,990 --> 00:06:20,490
and when I built it I just hit command B.

77
00:06:20,570 --> 00:06:25,790
You can also pull down the product and select build

78
00:06:30,810 --> 00:06:32,020
what this is building.

79
00:06:32,040 --> 00:06:39,150
Let's go over to the documents and let's go ahead and click on the models here underneath the swift

80
00:06:39,660 --> 00:06:42,510
models and we'll start reading about this stuff.

81
00:06:42,550 --> 00:06:51,090
OK so when you create your class that inherits from object really the only restrictions that you have

82
00:06:51,270 --> 00:06:58,470
buy like you can make protocols you can make any function that you need to manipulate your object or

83
00:06:58,470 --> 00:07:00,260
save things whatever you want.

84
00:07:00,300 --> 00:07:04,860
The only thing you have to do is always make sure you access that object on the same thread.

85
00:07:04,890 --> 00:07:13,530
So when we start saving to our to our realm database we're going to make sure we create a a constant

86
00:07:13,530 --> 00:07:20,370
for doing the dispatch queue and will always access our role model from the same thread.

87
00:07:20,490 --> 00:07:28,260
And that's the that's the only like kind of strange thing with relm but it's no big deal we'll do it

88
00:07:28,440 --> 00:07:30,060
and we'll move forward.

89
00:07:30,300 --> 00:07:35,940
And so as you're creating your objects so you need to inherit from object which is cool.

90
00:07:35,970 --> 00:07:36,940
It's fine.

91
00:07:37,290 --> 00:07:42,070
And then we are a little restricted with supported types.

92
00:07:42,420 --> 00:07:49,620
And with that being said you can use bhool integer and basically any ints you want you can use double

93
00:07:49,620 --> 00:07:56,080
and float which is awesome and strings so those are the primary ones are easy to use in.

94
00:07:56,610 --> 00:08:02,600
But for like a. date you had to use anti-state you can't use date and then same with data ASC-P and

95
00:08:02,620 --> 00:08:06,600
as data and we'll get into all of this.

96
00:08:06,630 --> 00:08:13,560
And as you can see like C.G. float is discouraged and like we're going to be keeping track of locations

97
00:08:13,560 --> 00:08:14,370
eventually.

98
00:08:14,580 --> 00:08:19,320
And that C-L location and I'll show you how we can get around all that.

99
00:08:19,320 --> 00:08:24,640
And when you actually dive deeper into like sea location it's really just two doubles.

100
00:08:24,780 --> 00:08:28,300
So it ends up not being a problem and we'll get into all this.

101
00:08:28,310 --> 00:08:29,830
It's not too hard.

102
00:08:30,150 --> 00:08:35,580
And then so with all that being said with supported types and everything why don't we go dive back in

103
00:08:35,610 --> 00:08:37,300
and we'll start making our model.

104
00:08:37,320 --> 00:08:41,180
And as we run into things I'll I'll explain it the best I can.

105
00:08:42,290 --> 00:08:48,420
So if we go back to a run model we have it inheriting from objects and we also.

106
00:08:48,430 --> 00:08:58,630
So when we create a a variable for Rome and needs to be dynamic it needs dynamic in front of the variable

107
00:08:58,690 --> 00:09:08,810
so it can be in a successor to the underlying database for relm and that allows the database to change

108
00:09:08,810 --> 00:09:10,090
our variables.

109
00:09:11,480 --> 00:09:16,550
Like are normal variables and we don't have to deal with anything on the database level realm does it

110
00:09:16,550 --> 00:09:17,600
for us.

111
00:09:17,600 --> 00:09:20,090
So just remember that dynamics required.

112
00:09:20,300 --> 00:09:30,760
And it allows our variables to you know be grabbed and saved and changed just like it's a local a local

113
00:09:30,760 --> 00:09:38,580
there or a native variable just like you know doing something like var number equals one.

114
00:09:38,600 --> 00:09:40,600
It would be just like that.

115
00:09:40,810 --> 00:09:48,410
But dynamic we're allowing it to be assessed by the underlying database and saved to the database the

116
00:09:48,410 --> 00:09:51,200
same way.

117
00:09:51,320 --> 00:10:00,600
So our first variable that we're going to need so this is important to know when you're using databases

118
00:10:00,930 --> 00:10:06,300
you need IDs so you need an I.D. that you can always grab this object from.

119
00:10:06,300 --> 00:10:07,210
OK.

120
00:10:07,290 --> 00:10:09,680
And so we need to create an ID.

121
00:10:09,990 --> 00:10:16,810
And right now we're just going to make this public private set and var ID.

122
00:10:16,950 --> 00:10:25,380
And it's always best to just set the variable to a default like an empty string or 0 or whatever you

123
00:10:25,380 --> 00:10:32,490
want it just makes everything easier for relm to have a variable already set just in case it doesn't

124
00:10:32,490 --> 00:10:32,850
get set.

125
00:10:32,850 --> 00:10:37,620
You can have optionals it just it complicates things a little bit.

126
00:10:37,620 --> 00:10:44,750
So for this project we're going to set all of our variables to a default variable for now.

127
00:10:45,000 --> 00:10:45,280
All right.

128
00:10:45,330 --> 00:10:52,710
And so what public and private means it's just like data encapsulation but avoids us having to do our

129
00:10:52,710 --> 00:10:53,950
getter and setter.

130
00:10:53,970 --> 00:10:59,580
And I know in a lot of our other courses we always do getters and setters but this is the same exact

131
00:10:59,580 --> 00:10:59,890
thing.

132
00:10:59,910 --> 00:11:01,810
It allows the variable to be good.

133
00:11:01,920 --> 00:11:08,310
Or you know you can get it from a different class or file but then you can only if you do private and

134
00:11:08,310 --> 00:11:12,740
then set in parentheses you can only set the variable within this file.

135
00:11:12,840 --> 00:11:19,030
So it's super important and it's the same thing as data and cancellation is just a lot less typing.

136
00:11:19,080 --> 00:11:28,380
So we need an ID and we also need a dynamic public.

137
00:11:28,380 --> 00:11:31,950
I have to spell dynamic right dynamic

138
00:11:34,290 --> 00:11:39,040
private man struggles falling off Homero.

139
00:11:39,570 --> 00:11:40,290
All right.

140
00:11:40,410 --> 00:11:44,610
And we also need a pace and we'll just set that equal to zero.

141
00:11:44,880 --> 00:11:46,380
And that's an integer.

142
00:11:47,070 --> 00:11:48,750
And then we need a

143
00:11:53,230 --> 00:11:54,940
we need the distance.

144
00:11:55,330 --> 00:11:59,980
And this is going to be a double and then we also need

145
00:12:04,040 --> 00:12:05,180
the duration.

146
00:12:05,270 --> 00:12:06,700
And that is also an integer.

147
00:12:06,710 --> 00:12:12,590
So we can just set that to zero Okay.

148
00:12:13,040 --> 00:12:15,160
So one thing we're probably forgetting.

149
00:12:15,170 --> 00:12:25,750
Let me just pull up our docs real quick when we look at our log we also display the date of the run.

150
00:12:26,000 --> 00:12:31,940
So we're going to also want to keep track of the date.

151
00:12:34,170 --> 00:12:38,640
And what's nice about if we keep track of the whole and best date.

152
00:12:38,970 --> 00:12:46,520
So if I do something like date it was created and whole and as Date object we will set it to.

153
00:12:46,920 --> 00:12:49,960
This is going to allow us to also sort.

154
00:12:50,010 --> 00:12:56,830
We can also sort all of our data by the date and the great thing is this date has a time stamp as well

155
00:12:57,120 --> 00:12:59,100
even if we don't display the time stamp.

156
00:12:59,100 --> 00:13:01,800
We can still display everything in order.

157
00:13:01,920 --> 00:13:04,640
All sorting off the next date.

158
00:13:04,650 --> 00:13:05,820
So that's really cool.

159
00:13:05,850 --> 00:13:08,680
So I think this is everything we need.

160
00:13:10,380 --> 00:13:19,020
And then with relm you have to have a primary key and you have to let relm know what variable is your

161
00:13:19,020 --> 00:13:19,840
primary key.

162
00:13:19,840 --> 00:13:20,390
OK.

163
00:13:20,670 --> 00:13:22,630
So we're just going to override.

164
00:13:22,630 --> 00:13:30,140
You have to do override class phunk index proc index.

165
00:13:30,150 --> 00:13:31,180
I'm sorry.

166
00:13:31,180 --> 00:13:33,900
A primary key I'm getting in the head of myself.

167
00:13:34,200 --> 00:13:42,900
Primary Key and this just returns a string and all you do in this is return the string for what variable

168
00:13:42,900 --> 00:13:51,840
you want to be the primary key and we just return it as a string and you just type exactly what your

169
00:13:51,840 --> 00:13:55,980
ID is or whatever variable you're using and your idea is to be unique.

170
00:13:55,980 --> 00:14:03,640
So you can't just like when you create an object or you couldn't just say the ID is run but you could

171
00:14:03,640 --> 00:14:11,630
say your ID is run 0 run 1 run to run 3 and have incrementing numbers to keep track of your ID.

172
00:14:11,710 --> 00:14:12,870
But it has to be unique.

173
00:14:12,870 --> 00:14:14,350
It's just like a database.

174
00:14:14,380 --> 00:14:19,990
So like you can grab any object out of the realm database just knowing its ID.

175
00:14:20,020 --> 00:14:21,140
So that's really nice.

176
00:14:21,150 --> 00:14:22,350
It's really powerful.

177
00:14:22,390 --> 00:14:23,850
It's really cool.

178
00:14:24,100 --> 00:14:28,060
And then another cool thing is indexed properties.

179
00:14:28,110 --> 00:14:35,020
Override class func and then indexed properties.

180
00:14:35,680 --> 00:14:39,640
And this returns an A an array of strings

181
00:14:43,500 --> 00:14:50,670
and all we do here is return an array and pass in the index properties that you want to use.

182
00:14:50,670 --> 00:15:00,900
So like in our case an index property is like if you want to filter or sort by something you would use

183
00:15:00,900 --> 00:15:07,680
an index properties so we probably want date pace duration and distance to all be part of these index

184
00:15:07,680 --> 00:15:09,450
properties right.

185
00:15:09,450 --> 00:15:14,400
The one downside in an index property does not work with type double.

186
00:15:14,400 --> 00:15:26,280
So we can do date pace and duration and you do that the same way as returning the key space loops and

187
00:15:26,280 --> 00:15:29,870
it doesn't matter the order what is going on.

188
00:15:30,270 --> 00:15:30,620
OK.

189
00:15:30,630 --> 00:15:34,950
So date and duration.

190
00:15:35,760 --> 00:15:36,680
Okay.

191
00:15:37,020 --> 00:15:40,850
And now we need a convenience initialiser

192
00:15:45,870 --> 00:15:55,890
so convenience and net and all we need to initialize a run object is going to be the pace which is an

193
00:15:55,890 --> 00:16:07,720
integer the distance which is a double and the duration which isn't right and then we just have to call

194
00:16:07,720 --> 00:16:08,640
a self in it.

195
00:16:08,640 --> 00:16:09,940
So call it.

196
00:16:09,970 --> 00:16:19,900
It creates itself and then we can set the ID here and the ID we want to generate a random function or

197
00:16:19,900 --> 00:16:23,550
a randomly generated number right.

198
00:16:24,040 --> 00:16:34,120
So I'm going to kind of take a side turn so we can get this Id set up so let's go ahead and create this.

199
00:16:34,210 --> 00:16:40,220
OK so our idea we want to be a randomly generated ID right.

200
00:16:40,220 --> 00:16:44,010
And I don't want to think about it because you can't ever have the same number.

201
00:16:44,030 --> 00:16:49,220
So if we said this is run one we'd constantly have to be referencing the realm database to know what

202
00:16:49,220 --> 00:16:50,400
that count is.

203
00:16:50,480 --> 00:17:01,380
But I'm just going to make a random you ID and you can do that easily by just doing it you you Id open

204
00:17:01,590 --> 00:17:08,180
an enclosed in parentheses and then we want it as a string and we want it all lowercase and it doesn't

205
00:17:08,180 --> 00:17:09,930
matter we don't have to make it lower case.

206
00:17:09,950 --> 00:17:17,240
I'm just doing this because it's a preference of mine and that will set our ID and that makes our IDs

207
00:17:17,240 --> 00:17:19,000
always unique for our runs.

208
00:17:19,370 --> 00:17:25,430
And then we can just say ourself date and this is going to equal and this date and that will set the

209
00:17:25,430 --> 00:17:32,270
current date when this objects created and then self pace equals pace.

210
00:17:32,720 --> 00:17:38,750
And I'm doing the self because we need to know we're talking about these variables and not this variable.

211
00:17:38,750 --> 00:17:39,870
Right.

212
00:17:39,920 --> 00:17:40,210
All right.

213
00:17:40,220 --> 00:17:50,770
And then self distance equals distance and then self duration equals duration.

214
00:17:50,780 --> 00:17:52,330
All right.

215
00:17:52,510 --> 00:17:59,340
So we accomplished making our first relm object.

216
00:17:59,400 --> 00:18:00,040
OK.

217
00:18:00,120 --> 00:18:07,080
So everything we did here is basically the building blocks and everything you need to create a realm

218
00:18:07,140 --> 00:18:14,220
object and we'll end this course here or this lesson here and in the next one I'm going to show you

219
00:18:14,220 --> 00:18:22,840
how to add the objects to the database so we can grab it and display it on our log later.

220
00:18:23,010 --> 00:18:27,920
So in the next lesson we will be saving our object to the database and I will see you then.

221
00:18:27,930 --> 00:18:34,680
But before we sign off let's pull up our terminal and make sure we commit this so get ad period get

222
00:18:34,680 --> 00:18:43,180
commit Dasch am and we created our relm object.

223
00:18:44,190 --> 00:18:44,550
All right.

224
00:18:44,550 --> 00:18:44,940
Great.

225
00:18:44,940 --> 00:18:47,000
And I will see you guys in the next lesson.
