All right. 1
So in this module, we're going to look at how we can use Realm to persist our data, instead of using Core 2
Data. 3
If you have never come across Realm, then it's a good idea to familiarize yourself with it by going to 4
realm.io. And they've got a whole bunch of educational resources, as well as tutorials, example apps, 5
and also a really, really good documentation. 6
So what we're going to be using is the Realm database which is an alternative to SQLite 7
and Core Data, and it's also open source which is great because if you wanted to dig in and look at how they implemented 8
their core, then you can do that. 9
So we're going to head over to Products, Realm Database, and we're going to pick our language as Swift, 10
and this takes us to the Swift documentation. 11
So if you wanted to look at the source code, then you can go to Realm Swift and you can dig through all 12
of these files. But I'm going to show you how to get started. 13
So in order to install Realm, there's three ways. You can either download the Framework or use CocoaPods, 14
or use Carthage. 15
And I, as always, favor CocoaPods. 16
Look at how short it is compared to everything else. 17
And so this is what we're gonna be using. 18
So let's head over to our project. 19
We're going to close it. 20
Going to open up Terminal and we're going to locate our Todoey folder, 21
so the one that, of course, contained the Todoey.xcode project, and then we're going to cd to it. 22
And if you're interested in speeding up your workflow, you can actually change commands in the Terminal 23
together by just using a semicolon. 24
So we're going to change directory to the Todoey folder, and then we're going to perform "pot init" at 25
that location, and then we're going to say open Podfile in the application code Xcode, and we're gonna 26
hit enter. And now it should go through and perform all of those things bringing us to our Pod file. 27
o now I'm going to uncomment this line, so that we're targeting platform iOS 9.0 and above 28
and we are going to use frameworks. And the Pods that we're going to incorporate is one called 29
pod 'RealmSwift' and this has dependencies 30
so we'll also pull in the Realm framework as well. 31
So we're going to hit save, close that down, and go back in here, and say "pod install." All right. 32
So after a little bit of waiting, you can see it's now done, and it's installed the latest versions of 33
Realm and Realm Swift by just simply saying Pod Realm Swift. Now, currently, the latest version of Realm Swift 34
is 3.0.2 and that is the version that we're pulling in through CocoaPods as well. 35
So just check and confirm, and then we can close down Terminal and we're going to go back into Finder 36
and open up our text code workspace file. 37
All right. 38
So here's our brand new Xcode project with those pods incorporated 39
and let's just expand all of our folders so that we can see it clearly. 40
And as you can see, as soon as we install Realm, we've got this warning in our status bar here. 41
And it says that your pods including the Realm and Realm Swift need to be updated to recommend settings. 42
This is not a problem though because if we just click on this warning, then it will update all of these 43
things automatically for us. I'm going to hit perform changes and it's all good to go. 44
And that's it. 45
All right cool. 46
Okay, so the first thing that we're going to do is we're going to go over to our AppDelegate and we're 47
going to try and create a brand-new data entry in our Realm database. 48
So inside here the first thing that we need to do is we're going to have to import RealmSwift, and as 49
always, usually, with CocoaPods. It might not recognize your modules when you first open up your Xcode 50
workspace. 51
So if it does this, and you can see, for sure, that there it is, then you can simply go and hit command 52
B to build and it should get rid of that error for you once build complete. 53
Now, we're going to write our code inside the application didFinishLaunchingWithOptions delegate 54
method. 55
And here we're going to first create a brand-new Realm. So you can imagine Realms as almost like different 56
persistent containers. 57
And we're going to call it realm, 58
so let realm equals a new object from the Realm class. 59
Now that would be great if that just worked as is. 60
But unfortunately, the initialization of a new realm is actually marked with throw, 61
so it can, in fact, throw an error. 62
So we have to market with a try which means that we probably need a do catch block. 63
So, now that's a little bit safer. 64
We're going to use it to save a new piece of data. 65
So Realm allows us to use object-oriented programming and persist objects. 66
So let's go ahead and create a new Swift File and let's call it just Data and it's going to be grouped 67
inside the Data Model folder. And inside Data, we're going to be using Realm, 68
so I'm going to import RealmSwift, and then I'm going to create a new class. 69
The class is, of course, going to be called Data and it's going to subclass something called Object. 70
An Object is a class that's used to define Realm model objects. 71
So it's a superclass that we're going to use to enable us to persist our Data class. And the code inside 72
is going to be where we're going to declare our properties for the Data class. 73
So, say, if the data that we needed was a variable code name and it was of type String and it starts off 74
with a value of an empty string, and another one called age and it's of type Int and it starts off being 75
0. 76
So under any normal conditions if we weren't subclassing object and we were just creating a new class 77
to represent our data, then this would be fine. 78
However, because we're using Realm, we actually need to mark our variables with the keyword "dynamic." And 79
there's a long and short answer as to what dynamic means. 80
So the complex answer is that dynamic is what's called a declaration modifier. 81
It basically tells the runtime to use dynamic dispatch over the standard which is a static dispatch. 82
And this basically allows this property name to be monitored for change at runtime, i.e., while your app 83
is running. 84
So that means if the user changes the value of name, 85
for example, while the app is running, then that allows Realm to dynamically update those changes in the 86
database. But dynamic dispatch is something that actually comes from the Objective-C APIs. 87
So that means that we have to mark dynamic with at Objective-C to be explicit that we are using the 88
Objective-C runtime. 89
Now, the simple answer as to why we just added these two keywords in there is that we need to add these 90
two modifiers, basically, so that Realm can monitor for changes in the value of this property, 91
and this is what we'll allow us to do it while the app is running. 92
So let's go ahead and add that to our second variable as well, 93
so they are now both compliant with how Realm properties should be declared. 94
So whenever you're declaring a new property that's any of the basic data types, you will need these two 95
keywords in front of it. 96
Okay. 97
So, now we can go into our AppDelegate and we can create a new piece of data that is going to be an 98
object of the Data class and we can set its properties by saying data.name 99
= "Angela" data.age equals-- I'm not going to say my real age here, 100
right? 101
Let's say, I'm 12. 102
All right. 103
So let's go ahead and add our new item, i.e., create in CRUD, and we're going to commit the current state 104
of our Realm. 105
So just as we did with Core Data, we basically created a new piece of data, 106
then we used the context to commit the current state to our persistent container, or in this case, it's 107
our Realm database. 108
So in order to do that, we can move this down here. 109
And after we've created this brand-new Realm, we can then say realm.write 110
which means commit the current state persistent storage or to our Realm database, and then we can say 111
realm.add a new object, and a new object is going to be our data. 112
So as with initializing a new Realm, realm.write can also throw. 113
So we have to also market with the "try" keyword which is why they're both kept inside this do block underneath 114
where we created our data. 115
So, now all we have to do is run our app so that our application didFinishLaunching gets called and 116
all of this gets implemented so that we'll be able to save our data to our Realm database. 117
All right. 118
So that worked without any issue. 119
But how do we view our data because we're not using it to populate any of our table views? This is still 120
using Core Data, 121
remember? And all of our code is inside the aapp Delegate. 122
But if we have a look in our debug log, we didn't get any errors coming back, right? 123
So that means our process probably completed successfully. 124
So our data did get saved into our Realm. 125
So in order to locate where our data is stored or where our Realm database is, we can use a print statement. 126
We can say print (Realm.Configuration. 127
defaultConfiguration.fileURL. And this is the location of the Realm file. 128
And now, if we print that out, we'll be able to locate where our Realm database exists. Now, because we're 129
not unwrapping it, 130
it's gonna get printed as an optional. 131
And there it is. 132
So that's our file location. 133
But before we can view this file, you can see that the file is actually something called 134
default.realm. 135
So it's a Realm file. 136
So in order to see the data that exists inside, we need to download a piece of software from the App 137
Store. 138
And it's a free Mac app called the Realm Browser. 139
So if you search for Realm Browser inside your App Store, 140
then you'll come across this Mac app made by the round team and it's free to download, 141
and it's quite a small piece of software. 142
Only, what, 5.9 megabytes. And you can simply download it onto your local machine. 143
But once you have done so, then you can open up your Realm Browser and you can click on the open realm 144
file. 145
So, now it's going to take you into your file browser and we're going to have to navigate to that location 146
that we printed out earlier on. 147
Now, previously, I've always shown you the sort of bog-standard way of navigating to a location in your 148
file directory which is chasing the directories, 149
right? 150
But it's a little bit painful. 151
So let me show you how you would do it as a shortcut. 152
So if you hit command shift G, then you can actually go straight to the folder or the document. And all 153
you have to do is take this string that says a file something, and all the way down to Realm without copying 154
any of the Realm brackets, hit command C to copy, and then we're going to hit command V to paste. 155
Now, if you navigate to the front of this string, if you delete the file, colon, dash, dash, so you only end 156
up with a forward slash, user, slash, 157
angelayu, this is your file path that will help the Finder to navigate to this location. And then you can go 158
ahead and simply hit Go, and it takes you straight to that location. 159
Now, you might think I'm a little bit sadistic for making you guys go through this process many times 160
previously, but it's always good to know how it's done manually before you learn about shortcuts because 161
a lot of people learn the shortcuts first, and then when you get stuck or when something doesn't work, 162
then it's always very confusing. 163
So there's always the long path to fall back on, but to save your sanity and your precious life, then 164
it's a good idea to use the file path to navigate. And you can do this in any Finder, not just in the Realm 165
browser, 166
by the way. So if you're inside any sort of Finder window and you're trying to locate something, you can 167
simply hit command shift G and you can go straight to that location. 168
Now that we're here inside the Realm browser, we can simply select our default.realm database and 169
we can hit Open. And Realm will need access to this path, 170
so we're going to have to click Allow and you might even have to enter a password every now and then. 171
So we run our app twice, first time to check that it worked, 172
and the second time, to get this file path. 173
And that means that our application didFinishLaunchingWithOptions got called twice which means that 174
we saved two pieces of data both with name Angela and age 12. 175
And you can see that the Realm browser is this really, really simple interface where you can see your 176
models, 177
so our Data Model, and you can see your properties: name which is a string, age which is an integer. 178
And we've got our pieces of data and it even shows you the minimum, average, maximum, the sum, and some 179
simple bits of analysis on your data. 180
So that was pretty pain free, right? 181
And compared to how much work we needed to do in order to set up our Data Model using Core Data, 182
I hope you can see that this is a very, very useful tool. And we're going to dive deeper into Realm in 183
the next lesson, so that we can start working on how to do CRUD using Realm, instead of Core Data. 184
And you're going to see all of the ease and the shortness of the code and all of the beauty of this new 185
way of doing things. 186
So for all of that and more, see you on the next lesson.