What's up guys.
Jacob Bledsoe with Dev slopes dot com.
So in the last lesson we have everything completed as far as we need to have all the run data from one
run at a time.
But our problem is let's just run this real quick.
Our problem is when we start our run we have data right.
We have our pace and we have distance.
But when we end our run we don't have any data.
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
right.
So to begin we have to install the realm pod.
OK.
So we can actually we need to close out of our project for this.
So close this and let's see you got some extra stuff and pull up your terminal and Wellar at this I'm
going to go ahead and create this lesson's branch.
Less than 12.
OK.
And now we need to do a pod in it.
And that basically will install our pod or it will create our pod file and it will also create our workspace
instead of our project file.
So go do Padan it right.
And that is done now let's go ahead and just pull up our project.
So it's in my documents of slopes courses and treads.
And now as you can see it created our pod file.
I'm sorry I also said it would create our workspace but we have to add everything we need we need to
add our pod to our pod file.
Then when we install that it will create our our workspace that we would now work from.
So if we go ahead and say open this with a text editor Here's our APOD file.
Everything is perfect here.
We just need to now put our pod in for realm.
And so the best thing to do is let's go to the realm docs are right.
So over here if you go to a realm I go forward slash docs you'll get to this nice screen and we were
interested in the get started and click on installation and you can read through all this good stuff.
We were interested in swift right now if you want to set up relm for other things.
All the docs are here for you.
So this kind of jumps into an introduction with everything let's just click over here on the left side
get started and we're interested in installation for cocoa pods.
Right.
And all we have to do is use frameworks which by default.
We already are.
And then we need pod realm swift so I'm just going to copy and paste this POD Rhome swift and let's
go back to our pod file and right underneath use frameworks we can fix all this.
Once our projects created I'm just going to paste this here saved this text file and go ahead and close
out.
And now from our terminal when you're inside our project now we just have to do a pod install and there
we go.
Installed relm 2.8 too great.
And it's telling us that we should specify a version I think in our pod file it does 9 automatically
which is fine.
And there goes it created a pod file clock and our workspace code.
Now you always want to open the X-C workspace not the X code project anymore.
So go ahead and open the workspace code and it's our same project now it's a directory for POD's.
And if you just pull this down and go into the pod file you'll see that our pod of realms with does
install and I'm just going to tab that over and that's all we need to do.
And what it was actually yelling at us for it wants us to specify what was we're going to do if we just
get rid of that hash tag we'll say down to nine point OK.
And that should be good.
Let's go ahead and now for that to actually take take or start working we should do a pot install again.
But we shouldn't get that warning.
There we go and that warning's gone.
OK great.
Now if we just open up our project again expand the treads folder and let's see in our model folder
we need to create our run model.
And if we go to our current run VC we have our duration our pace and our distance that we need to keep
track of right.
And I'm pretty sure that's all we're going to need in our in our model right unless you guys can think
of anything else.
Which we might be running across shortly but let's go ahead and make our our model so go ahead and right
click model folder and say new file and we want ios in a swift file.
OK.
And we are going to call this run and hit create and now we have an empty and empty swift file and first
we need to import realm or relm Swift right.
And now we need to create our object which is going to be class run and it needs to inherit from object.
And let's go ahead and build this.
So it finds the realm swift
and when I built it I just hit command B.
You can also pull down the product and select build
what this is building.
Let's go over to the documents and let's go ahead and click on the models here underneath the swift
models and we'll start reading about this stuff.
OK so when you create your class that inherits from object really the only restrictions that you have
buy like you can make protocols you can make any function that you need to manipulate your object or
save things whatever you want.
The only thing you have to do is always make sure you access that object on the same thread.
So when we start saving to our to our realm database we're going to make sure we create a a constant
for doing the dispatch queue and will always access our role model from the same thread.
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
and we'll move forward.
And so as you're creating your objects so you need to inherit from object which is cool.
It's fine.
And then we are a little restricted with supported types.
And with that being said you can use bhool integer and basically any ints you want you can use double
and float which is awesome and strings so those are the primary ones are easy to use in.
But for like a. date you had to use anti-state you can't use date and then same with data ASC-P and
as data and we'll get into all of this.
And as you can see like C.G. float is discouraged and like we're going to be keeping track of locations
eventually.
And that C-L location and I'll show you how we can get around all that.
And when you actually dive deeper into like sea location it's really just two doubles.
So it ends up not being a problem and we'll get into all this.
It's not too hard.
And then so with all that being said with supported types and everything why don't we go dive back in
and we'll start making our model.
And as we run into things I'll I'll explain it the best I can.
So if we go back to a run model we have it inheriting from objects and we also.
So when we create a a variable for Rome and needs to be dynamic it needs dynamic in front of the variable
so it can be in a successor to the underlying database for relm and that allows the database to change
our variables.
Like are normal variables and we don't have to deal with anything on the database level realm does it
for us.
So just remember that dynamics required.
And it allows our variables to you know be grabbed and saved and changed just like it's a local a local
there or a native variable just like you know doing something like var number equals one.
It would be just like that.
But dynamic we're allowing it to be assessed by the underlying database and saved to the database the
same way.
So our first variable that we're going to need so this is important to know when you're using databases
you need IDs so you need an I.D. that you can always grab this object from.
OK.
And so we need to create an ID.
And right now we're just going to make this public private set and var ID.
And it's always best to just set the variable to a default like an empty string or 0 or whatever you
want it just makes everything easier for relm to have a variable already set just in case it doesn't
get set.
You can have optionals it just it complicates things a little bit.
So for this project we're going to set all of our variables to a default variable for now.
All right.
And so what public and private means it's just like data encapsulation but avoids us having to do our
getter and setter.
And I know in a lot of our other courses we always do getters and setters but this is the same exact
thing.
It allows the variable to be good.
Or you know you can get it from a different class or file but then you can only if you do private and
then set in parentheses you can only set the variable within this file.
So it's super important and it's the same thing as data and cancellation is just a lot less typing.
So we need an ID and we also need a dynamic public.
I have to spell dynamic right dynamic
private man struggles falling off Homero.
All right.
And we also need a pace and we'll just set that equal to zero.
And that's an integer.
And then we need a
we need the distance.
And this is going to be a double and then we also need
the duration.
And that is also an integer.
So we can just set that to zero Okay.
So one thing we're probably forgetting.
Let me just pull up our docs real quick when we look at our log we also display the date of the run.
So we're going to also want to keep track of the date.
And what's nice about if we keep track of the whole and best date.
So if I do something like date it was created and whole and as Date object we will set it to.
This is going to allow us to also sort.
We can also sort all of our data by the date and the great thing is this date has a time stamp as well
even if we don't display the time stamp.
We can still display everything in order.
All sorting off the next date.
So that's really cool.
So I think this is everything we need.
And then with relm you have to have a primary key and you have to let relm know what variable is your
primary key.
OK.
So we're just going to override.
You have to do override class phunk index proc index.
I'm sorry.
A primary key I'm getting in the head of myself.
Primary Key and this just returns a string and all you do in this is return the string for what variable
you want to be the primary key and we just return it as a string and you just type exactly what your
ID is or whatever variable you're using and your idea is to be unique.
So you can't just like when you create an object or you couldn't just say the ID is run but you could
say your ID is run 0 run 1 run to run 3 and have incrementing numbers to keep track of your ID.
But it has to be unique.
It's just like a database.
So like you can grab any object out of the realm database just knowing its ID.
So that's really nice.
It's really powerful.
It's really cool.
And then another cool thing is indexed properties.
Override class func and then indexed properties.
And this returns an A an array of strings
and all we do here is return an array and pass in the index properties that you want to use.
So like in our case an index property is like if you want to filter or sort by something you would use
an index properties so we probably want date pace duration and distance to all be part of these index
properties right.
The one downside in an index property does not work with type double.
So we can do date pace and duration and you do that the same way as returning the key space loops and
it doesn't matter the order what is going on.
OK.
So date and duration.
Okay.
And now we need a convenience initialiser
so convenience and net and all we need to initialize a run object is going to be the pace which is an
integer the distance which is a double and the duration which isn't right and then we just have to call
a self in it.
So call it.
It creates itself and then we can set the ID here and the ID we want to generate a random function or
a randomly generated number right.
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.
OK so our idea we want to be a randomly generated ID right.
And I don't want to think about it because you can't ever have the same number.
So if we said this is run one we'd constantly have to be referencing the realm database to know what
that count is.
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
an enclosed in parentheses and then we want it as a string and we want it all lowercase and it doesn't
matter we don't have to make it lower case.
I'm just doing this because it's a preference of mine and that will set our ID and that makes our IDs
always unique for our runs.
And then we can just say ourself date and this is going to equal and this date and that will set the
current date when this objects created and then self pace equals pace.
And I'm doing the self because we need to know we're talking about these variables and not this variable.
Right.
All right.
And then self distance equals distance and then self duration equals duration.
All right.
So we accomplished making our first relm object.
OK.
So everything we did here is basically the building blocks and everything you need to create a realm
object and we'll end this course here or this lesson here and in the next one I'm going to show you
how to add the objects to the database so we can grab it and display it on our log later.
So in the next lesson we will be saving our object to the database and I will see you then.
But before we sign off let's pull up our terminal and make sure we commit this so get ad period get
commit Dasch am and we created our relm object.
All right.
Great.
And I will see you guys in the next lesson.