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