What's up guys.
I'm Jacob whatso with Debb slopes dot com if you remember where we left off in the last lesson.
We created a list of co-ordinate locations for our run and we need this so we can get our our polyline
rendered on our map view.
And if you recall we ended up adding it down here in the location manager where we update locations
we just inserted it into the list and we ran into a slight problem because we created a new location
model with latitude and longitude and RELM wasn't saving anymore because we changed stuff in the database.
And once you create the realm database you need to take care of data migrations and everything.
The only other solution would be like deleting the app.
So you make sure you delete around files and then they would create new ones at the beginning but obviously
that's not a good solution for a production app.
So in this lesson we're going to go over creating a realm configuration file.
So first let's pull up our terminal and let's just create a new branch get check out flash B and this
will be less than 17.
All right.
Great.
And now we can go ahead and get started.
We won't commit anything yet because we haven't done a thing but let's go ahead and add a new file to
our utility folder.
It's going to be a swift file Iowas swift and let's call this our realm config just like that.
That's perfect.
All right.
And we need to import relm Swift.
So let's go ahead and do that and then we'll just call this class relm config.
Just like our file name
ok now.
Well all we need to do is basically make a like a static variable and we're going to use all of realms
configurations so we're gonna have to we have to set up the path where the realm database is going to
be saved and then we also have to keep track of the scheme number.
So every time you make a model change you would increase the scheme number in the configuration folder
and then you make a area that you would take care of all your data migrations.
We're not going to get into data migrations in this course but in a later more advanced course we'll
probably dive into them a little more.
So why don't we get started and let's just make a static variable so static var and we will call this
run data config and it's going to be of type realm that configuration and then go ahead and do opening
and closing brackets or curly braces.
And let's see the first first couple of things that we're going to need to do is we need to have a path
where we want our database saved.
So let's make a constant and we'll call it relm path and we're going to just basically save it in the
document directory of the phone.
So we'll just go ahead and say call file manager default.
You are Elle's right.
And this is going to be in the document directory.
So let's do that.
And then we need it to be in our user domain mask.
Right.
And we're going to just pull up the first slot in this array and we're going to go ahead and make a
new appending path and we're just going to call it our realm log which will make a.
So we'll call it we'll make it constant for it in a second realm relm run log.
OK.
So we need to make a constant for this.
And you know relm run log let's make it relm
run loops run config.
That makes more sense since this is the directory to our entire database.
And then all we need to do down here is do.
I'm sorry I went into our Constans folder and then let's go ahead had to make a constant variable called
relm underscore run underscore config and then just name this realm run config and just make it a string.
Save that.
And let's go back to our room config folder.
All right.
So now we have our path where we can save our items and now we need to create our our configuration
variable.
So we need a we'll just do we'll call this config and this is going to be equal to relm dot configuration
and we need to do an opening parentheses
around it and we'll do close in parentheses right away.
And now all we need to do is we need to declare our file you are all so open file you are a lot of this
won't autocomplete that's a little irritating and this is going to be our realm path that we just created
and then go ahead and hit Khama.
And we also need a scheme a scheme a version and let's see Scheme
version and here you would just put a number in and we'll start at zero.
And then so every time you would actually update your models you would have to increase this number
and that knows that the new scheme is different than the one saved in the realm database on your on
your local drive.
And if that's different you can run then run migration code where if you wanted to change.
I don't know a integer variable to a double or you could go through all that and instead of just creating
a new element you could actually replace the current one there and then we need to call our migration
Block.
And this is this is where all the migration data would be drawn.
And you just do it by migration.
And then our old schema
version in.
So it's a closure.
All right.
And here is where you would do an if statement.
And so if the old schema
old Scheme version is less than our current schema version which is zero will put 0 in and then you
know curly braces open and close in and just leaving this blank blank here nothing to do.
Relm will automatically detect new properties and remove properties.
So that's pretty cool.
So like we just added the Location property right.
So if we removed them as well as long as our schema number is different than the previous version it
will just replace either add elements or properties or delete properties.
The only time you actually have to do data in here is if your changing like types and you have a more
complicated migration which could be a target topic in its own and will probably will probably get there
because we do have to do that quite a bit and the depth slopes up.
So I think this will be a really good thing to go over.
It's just a little out of the scope for this beginner course.
And then we're just going to bring those parentheses up in front of the bracket and then underneath
your config variable here all we're going to do is return it return config and it should stop yelling
at us.
There we go.
So now we have this all done.
This is our configuration we stated where we're saving our database and then we also created our config
file.
So now if we go to let's see our run model and we have to find one of our rights like right here.
So right here where we just call relm that's just going go into the default setup which is fine in most
cases until you start updating your models and everything.
And you can also do encrypted relm databases which we do in our app and that would actually prevent
people from opening up the database file and been able to read stuff which will probably be covered
in another target topic as well.
But for calling our configuration file that we just created you would just do realm and then go down
to this one that says configuration.
And now all we have to do is call our around config that swift file and then call our run data config
and save that.
And now instead of calling just the default relm set up we're actually calling our relm config file
here which is pretty cool.
And now I'm just going to copy and paste this.
And we want to do it down here in our get all Ran's as well.
If we don't update this it won't actually find our runs because we're saving things to a different location
now and then let's see.
I believe our other one we don't have anything in our location object because we have a reference to
it right here with our list.
So that should be good.
And now let's go ahead and run this and we'll see if our run actually can save.
We have a weird error.
Oh I need to do a pod install.
OK let's run this again.
Write So you want to allow that.
And now if we go ahead and press Ron
make sure that we're in our city run so de-bug location city run and you see our stuff up in
it looks like we're driving a car.
Doesn't like something City.
Ron
we'll worry about that later.
All right let's go ahead and our Ron
and we did not get around mayor this time down here where it says unable to write around.
So that's great.
And you see that our last run popped up so that's a good sign.
And we also should have our run Lague
and we have a run log.
So that's really sweet.
Let's go ahead and stop that.
And I'm going to call this part done.
And in our next lesson we will be adding our polyline rendering to the map.
So that'll be really cool.
So I will see you guys next time.