So when you started building your app your number one goal was to build out a fancy UI,
right? But now that you've already done that it's time to create a database for your app.
That is exactly what I'm going to show you how to do with the help of the client-side data store
REALM from MongoDB. Make sure you SUBSCRIBE to the channel and let's jump right in. REALM is
a part of MongoDB and in this two-part series we are going to take a look at Realm first and
then we are going to sync with the help of Atlas Device Sync, which is also part of mongoSB. So make
sure that you take a look at the starter project it's at github.com/rebeloper/SwiftUIMongoDB.
I will leave a link in the description there are also going to be links for signing up for MongoDB
with some special offers right over there. So go ahead and check down in the description.
So what you want to do is select not the main but the starter branch and of course there will be
also final branch, if you want to take a look at the whole thing as after those these two tutorials
and what you want to do is just download zip or open with Xcode, whatever you'd like
and once you do that you will have this Xcode project open up. And there are quite a few files
right over here. We are going to go through all of them. What you need to know is that I have put them
in order, so this video it will be about local persistence and the next one will be about
Atlas Device Sync. So you can sync your data to atlas device sync with the help of MongoDB, okay.
So this will be about Realm, so we want to add in the Realm Swift git. So what we need to do is just
copy this link out, it's under the SwiftUI MongoDB app right over there with online 10 and we
just go to project, package dependencies, click the plus button and add that get url right over there.
Make sure that you select master. Let's add package select both of them Realm and Realm Swift.
Let's add packages and there we go after a few seconds we have the Realm and Realm database 12.1.0
as of today for this tutorial. But most probably it will work just fine even in the future.
So first thing first we want to talk about how we create our objects in Realm. So right over
here we have a group and an item model. Let's take care of the group first. So what
we are going to do is basically have a list of items inside the group. That's our whole
project by the way let me just select my phone right over here the device and this is kind of
the basic project that it's going to be built out. It's just a simple text. Let me just go right
over here and you will see that it's an image with a leaf and "Hello, MongoDB". Let's just
wait for the simulator to startup and here we go. This is our starter project, okay. Let's take
care of the group and the items. First of all of course you need to import a Realm Swift and we're
going to import Realm Swift quite a lot in this project. So we're going to create a final class
and let's just name this group and it needs to be an object and an object
key identifiable because of SwiftUI. So it will have an ID. So object key, object key identifiable.
Okay! There are two things that we need to add. First thing we need to take care of the ID
and that is something called underscore ID and of type object ID in Realm. So let's just go persisted
primary key and let's set this to true. Var underscore ID of type object id, there we go object ID.
And finally we want to add a list of items. So again let's just type that out. Persisted var
items and as you can see we are already creating the relationships right over here.
So it would be RealmSwift.list of item, okay. And let's initialize that. Now of course the items
will cannot be found. So let's just jump into the next file right over here and let's create
our item also. Again let's just import Realm Swift, let's create the final class and this
will be item. Again of type object and object key identifiable, there we go. Object key identifiable.
Let's set the ID. I'm just going to copy it out, so we save a little bit of time right over here
and here I'm going to add the items name and I'm also going to connect it
to our group, okay. So first thing is the name. So again at persisted and just simply var
name and if you are going to take a look at our helper files you will see a lorem ipsum and we
are going to grab a random name from that folder. Go ahead and check out that helper file, okay.
And finally let's just create that connection with our group. Again the persistent and this time
around I'm just going to use the origin property and I'm going to set items because right in
the group right over here I set the variable name to be items, that's great. And then var
group and of type and this will be a linking objects of type group, okay.
And those are our models, pretty simple right. So let's just move on to our views because we are
going to still have quite a lot of coding on the error view we are not going to change anything but
we are going to have an item details view where we are going to actually change the name, edit
the name of our item. So what do we want to do here? Again these items will be inside the list
you will see in just a second. So first of all as usual import Realm Swift and then we want to grab
that item that we want to add it, and that will be easily done with the
observed Realm object. This is the one. So var item of type item and you will see how we are going to
pass it down from the list when we are going to get to our next file. Finally we want to add in
here not just the text but we want to have a text field because now you will see how easy it is to
just link the item's name. I'm just going to have the place order, a title actually to new name
and I'm going to have a text and as you can see we need a binding. Couldn't be easier,
we just go item dot name and that's it, believe it or not that's it. This is really, really super cool,
it's SwiftUI like easy to use. And finally I'm also going to add the navigation bar title.
Let's just use the one with the thrill protocol they just have bar title right over here.
Of course navigation title will also work. Item dot name, okay. So that's it for our item details view.
We will be able to edit our item's name. Let's take a look at the items view because right over here
we want to and let's just scroll all the way up, we want to actually have a list of items but before
we do that let's just grab our group so we can get the items from that group. Right over here again
let's just import Realm Swift as usual and here we want to add in our group. Now this is done with
@observed Realm Object var and let's just name this group of type group, simple. Now if the group has
any items then we are going to present a list, otherwise we want to add in a text and let me
just add in that if statement. So if group dot items dot count equals zero then we are going
to add that text field otherwise we are going to add in a list and I'm just going to simply
copy out that simple text because yeah there's no reason to waste time on just typing that out.
So basically tap on plus to add new items. Now on the else part we are going to have a list.
So let's populate that list. First of all let's create our list right over here and of course we
are going to have a for each as usual. Now inside the forage we are going to have a group dot items.
Couldn't that be easier. Of course we do need to add our item in right over here
and what I'm going to do is just going to create a cell with an item and I'm going to pass that item
along. I'm going to create that in just a second, well actually I'm going to create it right now
because otherwise we're going to have that error all the way. So cell
item of type item, it's going to return us some view with a capital V please and let's just
return an empty view currently, so we don't have any errors, okay. So the cell, we are going to take a
look at it just in a second and now we want to add in onDelete and onMove functionality and
this couldn't be easier again. As you can see it's built for SwiftUI. So I'm just going to add in
onDelete and here we, well I'm not going to use the perform but I'm going to use dollar group
dot items and remove and as you can see the remove functionality it's asking us an offset
that's already inside the perform. So we can just simply remove that part
and that should be great, okay. Now the same goes for onMove, onMove perform again, group
dot items and move. At this time I'm just going to not have the auto completion
perform that. And believe it or not that's it. For onDelete and onMove. Let's finish this file by
adding the cell view and basically this will be a simple navigation link. I'm just going to paste it
in there, there we go. It is going to go to our item details view and it's going to use our
inputted item and the label will be the item name. Finally how do we add a new item?
It's in the add button right over here in the primary actions add button,
edit button and the logout button. The logout button we are going to talk about it in the
next video. Go ahead and also check that out. And right over here we are going to add a new item
by grabbing our group dot items and dot append, you wouldn't you believe it and I'm just going
to initiate a new item because if you remember the items are initiated with the simple lorem ipsum dot
random name. And basically that's it. We are done with the items view. Now because this is a more
complex project we are going to finish today's video up with the local only content view and the
next one will be about syncing with Atlas Device Sync. So let's just take a look at this local only
content view. First of all as usual we want to import Realm Swift and what do we want to do here?
Well first of all we want to see if we have any groups. So let's just go ahead and add in our
observed results, there we go. Observed results of type group dot self and var group.
Okay! So we got our observed results. Let's take care of our empty view in this body. So first
of all we want to see if we got any groups. So if let group again this is in singular equals
groups, group. Oh, I meant to have your groups, yes. That's why I'm telling you this will be groups the
plural. So groups dot first, if we do have a group then we are going to do something with that group
otherwise we are going to actually create it but of course because this is a view we want to add
in a progress view until we are going to create it. And on that progress view when this progress
view appears so the onAppear we are going to go dollar groups dot append and a new group. So group,
just like that. Once we do have our group we want to grab a navigation view. Of course because you
know on that list we have navigation links so we want them to be navigatable. So we have our
navigation view and we just add in our items view with our group, yes, there we go. And we just pass
the group along, okay. So let's see how this looks like. First of all let's go into the main
and what we want to do first is just set this RealmSwift.app to be nil because we are also
going to take a look at sync but that's for our next video. Go ahead and check it out at the end of
this one. For now I'm just going to add empty view right over here and let's remove this nice
horizontal stack. I did like that horizontal stack, okay. Yes, we do need to import Realm Swift to be
available and because Realm Swift also has an app class, app property we want to have SwiftUI dot
app, okay. Let's see that. Let's hit Command + B so we know that everything went okay, and now let's just
start a test. Let's build and run. Tap plus to add new items. Let's just tap plus right over
here and let's add some more and as you can see we have some random names right over there.
Let's go and add it our name. Let's just go with two right over here, items, there we go.
Buck Kinnear2 and also I want to edit this, maybe let's just delete this one
and maybe let's just move this one right over here. Okay, done and now if I build and run again
we should see the changes persisted which is as it is, okay. That's it for Realm
with MongoDB of course. Go ahead and check out the links in the description and if you do want
to know how to use Atlas Device Sync go ahead and check out our next video right over here.