All right. 1
So in the last lesson, we managed to get this far. We've got a to-do list where we're able to 2
add items through the UIAlertController, and we've also got this table view that allows us to check 3
and uncheck items in our to-do list. 4
Okay. So I mentioned in the last episode that I was going to show you how we actually have some bugs in 5
our app. 6
And the problem occurs when your app actually disappears from the memory of the phone. 7
So if we have a look inside app delegate, now we've not really talked about many of these delegate methods, 8
but this is a good time to do so because we're going to use it to show what's actually happening 9
when your app is going into the background. 10
So the first thing here, this method where it says didFinishLaunchingWithOptions. 11
This is a place--this gets called when your app gets loaded up, so it doesn't matter if the rest of your 12
app is going to crash. 13
This is the first thing that happens and this happens before the viewDidLoad inside the first view 14
controller or the initial view controller. 15
So if we make a print statement here and says didFinishLaunchingWithOptions, so that we can see when 16
this gets called. And the next thing is this one which is applicationWillResignActive. 17
And this tends to get triggered when, say, something happens to the phone while the app is open, i.e., in 18
the foreground. 19
So, say, if the user receives a call, 20
this is the method where you can do something to prevent the user losing data. 21
So, for example, say, if they were filling in a form in your app, and in the middle of it, they get a call 22
from somebody. 23
Now, it would be really annoying if by the time they get back to your app that your app was actually been terminated 24
and they've lost all of their data. 25
So that's another time point. 26
Now, this is another one that we're going to look at today. 27
Now, this is applicationDidEnterBackground. And this happens when your app disappears off the screen, 28
so when you press the home button, for example, or when you open up a different app. It's like a deck of 29
card, there's another card that's being placed on top of the deck, 30
so your app is no longer visible and it's entered the background. 31
Now, the other one that is super important is the applicationWillTerminate. 32
And this is the point where basically your app is going to be terminated. 33
Now, this can be user-triggered or it can be system-triggered. 34
So, say, if the user has pressed the home button to quit your app, and they decide to go into a different 35
app, 36
If this app happens to be a really resource intensive app, say, a game like Hearthstone or something, then 37
it might reclaim a lot of the resources that are being hogged by your app. 38
Even though it's in the background, it might still have processes that are running and is using up the 39
precious memory of the iPhone. 40
So in those cases, when the resources gets reclaimed from your app, then it goes from being in the background 41
to being terminated or assassinated, basically, by the operating system. 42
Now, let's run our app again and see when each of these messages gets triggered. 43
So the first thing that happens is didFinishLaunchingWithOptions, 44
and that happens at the moment when your app first loads up. And then the next thing is that if we press 45
the home button, your application goes into the background. 46
And similarly, if I double click and go to a different app, your application also enters the background 47
because it's no longer the thing that you see on the screen. 48
So the next one is how do you terminate your app, right? 49
Well, this can happen because the operating system triggers it because it needs to reclaim those resources 50
that precious memory or the user might trigger themselves by force quitting your app. 51
So if you double click on your home button, now sometimes it doesn't work very well in the simulator, 52
so just bear with it, but try it a few times and you should get to this type of screen. 53
And this is a place where you can browse through all of the apps that are still in the background. 54
But you can also hit it and swipe it up to quit, to force quit that app. 55
And that's the moment where you'll get "applicationWillTerminate." 56
And that is this delegate function. 57
Now, some of the logical things you might want to do, for example, is that, you know, in your form-filling 58
application, you'll probably want to add some safe method to your "applicationWillTerminate" or even 59
better to applicationDidEnterBackground because it's such bad user experience to go back into your 60
app and find that all of the data that you've been entering for the last five minutes has just now disappeared. 61
It's a sure way to get bad reviews. 62
So your app has a distinct lifecycle where it goes from being launched to being visible on the screen 63
to entering the background, and finally, being destroyed and the operating system reclaiming the resources 64
from it. 65
So just as we have life cycles, we were born, we live, and we die, 66
so do apps. 67
So, now that we've cleared up what most of these delegate methods do, I want to show you where the problem 68
lies in our app. 69
Now, we know that in previous modules, I tend to get rid of the bezel because that allows us to have a 70
bigger screen. 71
But in this case, because we're using the home button quite a few times, 72
so if you want your bezels to come back, you can always go to Window and Show Device Bezels to toggle 73
that. 74
Okay, so here's our app as it appears when it first loads up. And I'm going to add a new to-do item. 75
And when I hit Add Item, you'll see it appear in our table view 76
because we're adding it to our array and we're reloading the table view. 77
Now, if I hit the home button and my app enters the background, it still keeps a hold of that data in 78
the memory of the phone. 79
So when I reopen it, you can see that it still exists. 80
Now, the problem occurs when the app doesn't just enter the background, but actually terminates. 81
So all of scenarios where this might happen is, A: you're operating system trying to reclaim resources 82
because the user is trying to use like a really memory intensive app, 83
second option is if the user force quit your app. 84
Third option: if you installed an update to your app. 85
Fourth option: if the user updated their operating system. There are so many cases where your app is probably 86
going to end up terminating. 87
And when that happens, 88
so let's try that again. 89
Let's terminate our app. Application will terminate, 90
right? So, now it's gone from the memory of the phone. 91
And if we click on it again, you can see that new item that we added is now gone. 92
And this is because we don't have any form of persistent data storage. 93
We're only adding and appending to our array and that array is only going to be held in memory for as 94
long as this TodoListViewController exists. 95
And it's going to disappear and be destroyed if the application gets terminated. 96
So this is why we need persistent memory storage. 97
But there's actually quite a few options you have in terms of how you want to persist your data. 98
So in the next lesson, we're going to talk about one of the easiest ways of persisting small bits of 99
data, and that's through using NSUserDefaults. 100
So I'll see you there.