So in the last lesson, we manage to successfully implement our search bar, 1
but now we want to be able to go back to our original list of all of our items. 2
For example, when we click on the cross button, right? That's a normal behavior that users have gotten used to 3
when they clear the search bar. 4
So let's go into our TodoListViewController extension which is the part that deals with our search 5
bar methods, 6
and let's go and create a new delegate method here. 7
And the method that we're going to use is, of course, relating to the search bar. 8
And we're going to look for the delegate method that is searchBar textDidChange. 9
So this triggers the delegate method when the text inside the searchBar has changed, 10
so that means every single letter I type in here is going to trigger this delegate method. But also when 11
I go from a whole bunch of text to no text, this delegate method is also going to be triggered. 12
Now, I can isolate that particular instance by checking inside this delegate method. 13
So whenever there are any changes inside the text of the searchBar, I can see if the 14
searchBar.text.count which looks at the number of characters in the string or the searchBar text is equal 15
to zero. 16
And because of where this "if" statement exists, it means that text inside the search bar has changed, but 17
it has also gone down to zero. 18
So when we first load up the app and the search bar has no characters inside, this method, this delegate 19
method is not going to trigger because the text has not changed, and it's only when the text is changed 20
and it's gone down to zero 21
which pretty much describes the time point when I click that cross button 22
does this part of the code inside this "if" statement get triggered. 23
So inside here, I'm simply going to call loadItems, which if you' remember, have a default request that 24
just fetches all of the items from my persistent store, 25
just as we had up here in the viewDidLoad. 26
So, now if we run our app, and let's try and check it out, let's search for "Save," hit search, and then we're 27
going to click the cross button, and you can see that we are now reverted to our original list of items. 28
Now, this just one niggling thing. The fact that our keyboard has not dismissed itself and our cursor 29
is still flashing in the search bar, 30
now how can we get it to go back to the original state where the search bar doesn't have a cursor and 31
the keyboard is dismissed? 32
So, in order to do that, we need to call upon a method called resign first responder, 33
so telling the search bar to stop being the first responder. 34
Now, we're going to do it inside here after we call loadItems and we grab a reference to the 35
searchBar that we've got, 36
and we're saying resignFirstResponder which notifies this object, i.e., the searchBar that it has been 37
asked to relinquish its status as the first responder in its window. 38
Now, that sounds pretty dramatic, right? 39
It's like, you know, you're being overthrown or something. 40
But actually all it refers to is that it should no longer be the thing that is currently selected, 41
so no longer have the cursor and also the keyboard should go away because we are no longer editing it 42
anymore. 43
Basically, go to the background, go to the original state you're in before you are activated. 44
Now, whenever you're writing methods to affect the user interface, you tend to want it to tap in the 45
foreground. 46
Now, if we run the app and have an active session, if I click on the pause button here in the debug console, 47
you can see that we can trigger a debug session. And it's at this point that you can see that we've got 48
actually multiple threads that are running, but only one of them is the main thread in our queue. 49
So for example, if I decided to make a network call to, say, a Firebase database on the main thread, then 50
it means that it has to go over the Internet, grab the data back, 51
and no matter how long that process takes, it's going to freeze your app until it completes 52
which is why we tend to complete those tasks in the background using another thread which means the 53
user interface is not going to freeze up 54
while we're waiting for that process to complete. And on the background thread, once that process is complete, 55
then the data gets delivered back and we can update the user interface using the main thread. 56
But in our case, while background task are happening, we need to grab the main queue so that we can dismiss 57
our searchBar, 58
even if background tasks are still being completed. 59
So in order to do that, we have to tap into the dispatchQueue 60
and this is the object that manages the execution of work items. 61
So it's almost like when you're submitting an application for a visa, right, to an embassy, and you have 62
somebody at that embassy who is prioritizing who should be processing your visa. 63
So, for example, if you were a diplomat or you paid for, you know, extra fast service, then your passport 64
might get sent by the manager to the guy who's, you know, working 24/7 stamping visas, so that your visa 65
processing happens immediately. 66
However, if you are just, you know, just a standard person trying to get a visa to, you know, somewhere exotic 67
for the winter, then you might get shuffled to, you know, the guy who only works one hour a day, 68
and they have so many tasks lined up that it might take you a week before you get your visa processed, 69
again, and get your passport back. 70
So the DispatchQueue is that manager who assigns these projects to different threads. 71
And we're going to ask it to grab us the main thread. 72
And this is where you should be updating your user interface elements. 73
And then we're going to tap into a async method. 74
And inside this block of code, we're going to insert our searchBar updating code. 75
So whenever you're trying to move code inside Xcode, it's a little bit tricky because it's a matter 76
of timing. 77
You have to click on it and wait for the arrow to appear and the cursor to disappear before you start 78
dragging. 79
Now, you can move your code. 80
So now we're saying "Ask the DispatchQueue to get the main key, and then run this method on the main queue. 81
So, now if I run my app and we hit search, then we get back all the items that has the word "Save." But when 82
we dismiss the word "Save," then our keyboard pops away and our cursor disappears from our search bar. 83
All because of this line of code being run in the foreground. 84
Now, in the next lesson, we're going to take Core Data much further, and we're going to learn about one 85
of its main features which is establishing relationships or what we might call graphs inside Core Data. 86
And we're going to turn our simple to-do list into one that's more like a real-life to-do list 87
by having a separate to-do list for various categories. 88
So for all of that and more, I'll see you on the next lesson.