All right. 1
So, now that we've looked at create, read, update, and delete, it's time to look at how we can carry query 2
our Realm database. 3
And so we're going to be working within the search bar methods. 4
So let's go ahead and uncomment our extension and we're going to remove the parts that are Core Data 5
and we're going to, instead, make it work using Realm. So let's comment out all of these parts that use 6
NSFetchRequest or NSPredicate, NSSortDescriptor, 7
all of those NSs come from our Core Aata framework. 8
So instead, what we want to happen when the search bar gets clicked is we're going to filter our to-do 9
list items. 10
So we have to do items which is the array of results that consist of items and we're going to set its 11
new value to be equal to its previous value dot filter. 12
And you can see that this filter method from Realm takes an NSPredicate as an input, 13
so all we're saying here is that update todoItems = todoItems filtered by this predicate. 14
And we can specify the predicate format and arguments that we want to use for our filter. 15
So if we hit enter, then the format is going to be the same as this format which is, of course, the fact 16
that the title of the items CONTAINS, in big case,and diacritic insensitive, to the argument that we're 17
going to parse in, and the argument is going to be the searchBar.text, 18
and that is basically the equivalent of these two lines. 19
And we can tag on this sort by simply adding a dot and write sorted by KeyPath and ascending. 20
KeyPath is going to be title, 21
so that's what we're going to sort our results by and ascending will set to be true. 22
So it's alphabetical order, and that replaces all of this code. And we don't even need to call loadItems 23
because we've already loaded our to-do list items from the selectedCategory. And we're now just simply 24
filtering those items based on our search criteria and sort criteria. 25
So we can go ahead and delete all of that. 26
Now, the other part we need to fix is what should happen when we dismiss the searchBar? 27
So when the searchBar.text goes down to zero, we want to simply call loaditems, and loaditems, again, 28
looks at the current selectedCategory and pulls out the items sorted by the title in ascending order. 29
So there's actually nothing to change here. 30
So that is how easy it is to query our Realm database. 31
We can simply take a list of items and filter them based on a predicate. 32
And as we said in the core data module, you can take a look at the Realm NSPredicate Cheatsheet 33
and look at some of the tips and tricks and examples as to how you can query your Realm database using these 34
formatted strings. 35
So, now here's a challenge for you. 36
What if I don't want to sort my to-do list items by the title, instead, 37
I want it to be sorted by something called the "dateCreated" and I want the items that were created earlier 38
to show up at the top of the to-do list? 39
So can you fix this line of code? 40
But also, you'll need to add a new property inside our Item model in order to store that data, and that 41
data is going to be saved when we create a new item. 42
And finally, we're going to apply the sort over here. 43
Pause the video and see if everything that we've done makes sense to you, so that you can create this 44
new property and set it. 45
Now, you might need to look up stack overflow here and there, especially when you're trying to grab the 46
current date, but it's all very, very easy to find. 47
And I have faith that you will be able to work it out without a problem. 48
So pause the video and give it a go. 49
All right. 50
So how did that go? 51
So the first thing that we said is that we wanted it to be sorted by the KeyPath called "dateCreated." 52
So we have to go inside our Item data model and create a new dynamic variable that's called dateCreated 53
and it's going to be of type date, not data, and it's going to-- And I'm going to make it an optional. 54
So once we've done that, and then we have to go into our TodoListViewController and look at the place 55
where we create newItems. 56
Not only does our newItem get a title, but it's also going to get a new date. 57
And this is going to be set to equal the current dateat the time when this block of code gets called. 58
So let's go over to Google and let's try and see if we can figure out this problem. 59
So the problem I want is how to get current date in Swift, maybe Swift 3, because it's not a lot 60
of difference between Swift 3 and Swift 4. 61
So let's hit enter. And let's have a look at the first answer that we get back from StackOverflow. 62
How can I set a label's text to current date in Swift 3? 63
Sounds pretty much like what we wanted, 64
right? 65
And you can see that the top answer with 122 votes and also ticked off as the selected answer has 66
let date equal 67
new instance of date, and you can format it by specifying a particular date, month, year format. 68
Now, we don't actually care about the format, 69
all we want is just the date. So we can use that line right here, 70
newItem.dateCreated equals a new instance of Date. 71
And now every new item we create gets stamped with the current date and time. 72
So, now when we sort our to-do list items that are filtered, we can say sort by KeyPath "dateCreated." 73
And if we want the most recent items to show up at the bottom of the to-do list, then we have to keep 74
ascending as true. 75
And the last thing I need to do is to simply write tableView.reloadData. 76
That's it. 77
So, now let's run the app and see what happens. 78
So when you run your app, you might get this error, and you can see it's green which means that it's not 79
quite a crash. 80
And if you read it, it says something like 'try!' expression unexpectedly raised an error: an Error Domain 81
Code=10. 82
Migration is required you to the following errors. Item.dateCreated has been added." 83
Migration is required. 84
So to translate this what it's telling you is that you are adding this new property chordate created. 85
But remember, if you have a look at your previous items, they don't have that property, so Xcode and Realm 86
are both confused as to what they should do with this new property and what they should do with the 87
items that don't have that property. 88
And the solution to this is simply what they call migration. 89
And in terms of Xcode, all you need to do is go to the home, click and hold your Todoey app until it starts 90
jiggling, and then you click the cross to delete this instance of your app, and then you come back here 91
and you run your app afresh. 92
So, now if we go to this new Realm location, so remember we need the first forward slash, 93
and let's go over to our Realm Browser open, and we're going to command shift G, type in the path, hit 94
Go, click Open, click Allow. And now we find our new Realm. So you can see that this new Realm that we're 95
using for this instance of our app is completely empty. 96
There's no categories and there's no items. 97
So let's go ahead and add some, Home and Work. 98
And now we have two new categories: Home and Work, 99
but they have no items associated. 100
So let's go to Home and add some new items, Mop Floors, Buy Food. 101
This is always on my mind. 102
Not so much mop floors, but buy food is very important. 103
So we now have two items associated with our Home category and we can view them by clicking on this 104
link. 105
You can see now they both have this dateCreated property associated with them, and you can see that 106
I created Mop Floors on the 12th of December, 2017, at 14:21:01 seconds, and then four seconds later, 107
created Buy Food. 108
So, now if we add another one that contains the word "Food," so that we'll be able to sort it, 109
so maybe Buy Food and Eat Food. 110
Seems pretty logical, right? 111
Why would you buy the food if you're not going to eat it? 112
No, I don't actually have these items on my to-do list, 113
even though I'm so quick at coming up with them. 114
So, now we have Buy Food and Eat Food. 115
So I'm going to search for the word "Food." 116
And when I hit Search, then I end up with all of the items that contain the word "Food," but it's also sorted 117
so that the ones I created earlier gets shown at the top of the table view. 118
Now, if you didn't want that, then you can simply reverse the order by changing ascending to false. 119
But essentially, it's now looking at the time stamps of each of my items and applying the sort order 120
according to that dateCreated variable. 121
So, now if I hit the cross button, then I go back to the main table view, 122
and I have everything sorted here by alphabetical order. So feel free to have a play around with your 123
code and make your app do whatever it is that you wanted to do and familiarize yourself with how Realm 124
works with regards to CRUD and also how you can query the database.