So, let's quickly refresh ourselves by going through how our app works when it's using Realm. 1
So let's first go into our AppDelegate.Swift class and we can delete Core Data now and we can also 2
delete all of these Core Data saving related code 3
and I'm also going to get rid of my applicationWillTerminate. 4
Now, I'm going to leave my realm initialization here just to catch to see if there's any errors when 5
I initialize a new Realm. 6
And because we're not really using it here, we can simply make this an underscore, and delete that let 7
keyword as well, 8
and this print statement is also giving us a warning. 9
And in the future, we might need to find out where our Realm file is, so I'm not going to delete it, but instead, 10
I'm just going to comment it out. 11
So, now we have no warnings and we can safely save our AppDelegate. 12
Now, nothing really happens inside our AppDelegate. 13
So let's go and have a look at our Data Models. 14
The Category data model is the first one that we created and it is a Realm object, 15
so by subclassing this Object class, we're able to save our data using Realm, and we can specify for our 16
classes what properties it should have. And for the Category, it has a name property. It's going to be the 17
name of the Category. 18
And this is a dynamic variable. So we can monitor for changes in this property while the app is running, 19
i.e., during runtime. 20
Even if you look at the error's pane, you can see that the Buildtime errors and there's Runtime errors. 21
So this is before you compile and this is while your app is running. 22
And we've also got this relationship. And it specifies that each category can have a number of items 23
and that is a list of item objects. And Item objects are also subclassing the Realm Object and that allows 24
them to be saved using Realm as well. And the item is a little bit more fancy, it has three properties: 25
a title whether if it was done or not, 26
and also now it has a dateCreated property, 27
and finally, we specify the inverse relationship that links each item back to a parentCategory and we 28
specify the type of the destination of the link, 29
and we also specify the property name of the inverse relationship., and that relates to this property. 30
So, now we can go inside our CategoryViewController, and we can see that the first thing we did is we initialize 31
a new access point to our Realm database, and then we changed our categories from an array of category 32
items to this new collection type which is a collection of results that our Category objects. 33
And this is an optional so that we can be safe. 34
And when our view first gets loaded up, we load up all of the categories that we currently own. 35
So inside loadCategories, we set that property categories to look inside our realm and fetch all of 36
the objects that belong to the Category data type. 37
And then we reload our tableView with the new data. 38
And that reloadData calls all of these data source methods again and it returns the number of categories 39
as the number of rows. 40
And if this is nil, i.e., if we have no categories, then we simply return one cell, and that cell is going 41
to have a textLabel depending on whether, 42
again, if we have any categories. If we do, then we look at the categories results collection and we pick 43
out the one for the current indexPath.row, 44
and we use the name property to fill up that textLabel. 45
But if this is nil, i.e., if we didn't have any categories at all, then we simply fill that single cell 46
with the words "No Categories Added yet," and we return the cell. 47
Now, when we add a new item by clicking on the Add button, then we create an alert and the alert has a 48
text field and also an Add button. When we click on that Add button, 49
we create a newCategory that is a new Category object and we give the newCategory the name based on 50
what the user typed into the textField. 51
And then we call the save method to save this newCategory to our Realm database. And inside the save category, 52
we pass in that newCategory that we created here and we call this function called realm.write to commit 53
some changes to our Realm. And the changes we want to make is we want to add a newCategory to our Realm 54
database and we log if there are any errors. 55
Now, the next part is, what happens when we click on any of our cells? 56
Well, when we click on the cells, we fire didSeletRowAt indexPath and it performs a segue the withIdentifier 57
that takes us to the TodoListViewController. 58
But before we do so, we create a new instance of our destinationVC and we set the destinationVC's 59
selectedCategory property to the category at the indexPath.row that was selected which triggered 60
this particular segue. 61
Now, that takes us onto the TodoListViewController. 62
And when we set this selectedCategory, i.e., during that didSet block, we call loadItems which loads 63
up all of the to-do list items which, again, is a collection of results that are Item objects. 64
And this gets set to be equal to the selectedCategory?.items. 65
So remember that list of items inside Category? 66
Well, in the selectedCategory object, it also has a bunch of items associated with it. And those items, 67
then in turn get sorted by their title, and they are listed in alphabetical order. And then we reload 68
the tableView to call our Datasource Methods which specifies that we should have as many cells as we 69
have todoItems for our current selectedCategory. 70
But if there are no items for our current selectedCategory, then we just return one cell, and that cell is 71
going to have a textLabel based on the current item's title. And if the item.done property is true, 72
then we give that cell a checkmark, 73
otherwise, we give it nothing. 74
But if there are todoItems, i.e., this is nil, then the else block triggers, and that single cell 75
that we created up here gets populated with the text "No Items Added." 76
Now, in the TodoListViewContoller, when we press the Add Item Button inside our UIAlert, then we 77
look to see if our selectedCategory is nil or not, because it's an optional, remember? 78
And if it's not nil, then we set it to have this name called currentCategory. And then we try to update 79
our realm by creating a new item, setting the newItem's title, setting the dateCreated for the new item 80
to be the current date and time, 81
and then we set the currentCategory's item property to append this newItem to the end of that list. 82
And we commit all of that to our REalm database, and then we reload our tableView with all of that new data 83
that we added. 84
And when we select any of the cells, then we grab a reference to the item by looking into that collection 85
of results called todoItems, and we grab the item at the current selected indexPath.row, and then we try 86
to update its value by using 87
realm.write, 88
and we toggle the item.done property to be the opposite of whatever it used to be. 89
And then we, again, reload the data and deselect our cell. 90
Now, finally, when we select the search bar and we type something, and we click on the searchBarSearchButton, 91
then we update our collection of results called todoItems to a filtered version based on a predicate 92
which says that the title of the items must CONTAIN this argument, and the argument is whatever the user 93
entered into the searchBar, 94
and then we sort the items by the date that they were created in ascending order, 95
and then we reload our data. And if we dismiss our searchBar, then we call loadItems all over again. 96
And that basically resets todoItems to all of the items inside the current selectedCategory and it's 97
now sorted by alphabetical order. 98
So as you can see, by using Realm, instead of Core Data, we actually vastly simplified our code. 99
And I don't know if you'll agree with me, but I think the logic is also simpler. 100
There is no extra classes, there's no hidden classes. 101
All the lines of code are more human-readable like .filter, .sort, and it makes a lot more sense 102
when you're just glancing through the code. 103
So have a play around with Realm and it's completely your choice how you want to persist your data. And 104
if you prefer Core Data, then that's an equally valid solution. 105
Don't let me put you off it. But personally, I really, really like using Realm, and a lot of my colleagues 106
also feel the same. 107
And Realm has other benefits other than just ease of use. And you can have a look through the blog for 108
a lot of these reasons on why. 109
But, you know, in addition to its simplicity, it's also a lot faster than Core Data or SQLite. 110
So if you have a look at the number of queries you can make per second to your database, you can see 111
that Realm is almost double SQLite and it's way more than Core Data. So it has a lot of other benefits 112
as well and it's well worth looking through the documentation, seeing what other things can you do with 113
Realm. And, also, look at their example projects and see how far you can go with it. 114
So that's all from me for now. In the next few lessons, we'll be styling it up for our 115
Todoey app. 116
Now, those lessons are completely optional because we're only going to be dealing with front-end and UI stuff, 117
but we will be coding up and adding some libraries in order to make our app look beautiful as well as 118
work well. 119
So if you're interested, I'll see you for that on the next lesson.