1 00:00:04,939 --> 00:00:07,480 G'day everyone, welcome back. 2 00:00:07,480 --> 00:00:11,300 Now that we've created our TaskTimerViewModel class, 3 00:00:11,300 --> 00:00:14,040 we need to subscribe to the ViewModel. 4 00:00:14,040 --> 00:00:17,920 We do that in MainActivityFragment. 5 00:00:17,920 --> 00:00:27,960 We create a ViewModel instance first, as we saw in the previous section 6 00:00:27,960 --> 00:00:29,760 and we've got an error. 7 00:00:29,760 --> 00:00:33,240 ViewModelProviders isn't recognized. 8 00:00:33,240 --> 00:00:38,920 Your first mini-challenge is to work out why and to fix the problem. 9 00:00:38,920 --> 00:00:46,280 We did discuss this back in the previous section, so refer to that, if you can't work out why there's an error. 10 00:00:46,280 --> 00:00:51,400 Pause the video now and we'll continue in a few minutes. 11 00:00:51,400 --> 00:00:53,780 Did you work out what was wrong? 12 00:00:53,780 --> 00:01:00,360 We haven't added the dependency for the ViewModel and LiveData libraries to our build.gradle file. 13 00:01:00,360 --> 00:01:07,060 Let's have a look. 14 00:01:07,060 --> 00:01:15,140 Let's add in our missing code 15 00:01:15,140 --> 00:01:19,620 and then click sync now. 16 00:01:19,620 --> 00:01:27,120 This is also a good time to review the versions, to see if any of the components have been updated. 17 00:01:27,120 --> 00:01:30,280 Ok, we've got a ViewModel instance. 18 00:01:30,280 --> 00:01:43,500 Next, we need to subscribe to it. I'll do that in the onCreate function. 19 00:01:43,500 --> 00:01:54,680 Choose the Android.arch.lifecycle.observer import, if you're prompted. 20 00:01:54,680 --> 00:02:02,960 This is where we'll call our adapter's swapCursor function, to provide the new cursor, when we observe that it has changed. 21 00:02:02,960 --> 00:02:07,480 When the cursor changes, we pass the new one to swapCursor, 22 00:02:07,480 --> 00:02:10,139 causing the adapter to get the new data. 23 00:02:10,139 --> 00:02:17,080 We wrote swapCursor to return the old cursor, allowing us to call its close function here. 24 00:02:17,080 --> 00:02:21,060 That takes care of freeing up the cursor resources. 25 00:02:21,060 --> 00:02:25,200 We use a safe core, in case the return cursor is null. 26 00:02:25,200 --> 00:02:37,320 We can now run the app and we should see our tasks appearing in the list. 27 00:02:37,320 --> 00:02:41,260 That's looking good but it's still not quite finished. 28 00:02:41,260 --> 00:02:48,700 To see why, watch what happens when I add a new task. 29 00:02:48,700 --> 00:02:58,140 We're up to task 5, so I'm going to add tasks 6, 30 00:02:58,140 --> 00:03:02,040 and we'll give it the sort order of 6. 31 00:03:02,040 --> 00:03:10,340 When I tap on the Save button, we return to the task list but the new task doesn't appear. 32 00:03:10,340 --> 00:03:21,760 It has been successfully saved, as we can see if I close the app and then run it again. 33 00:03:21,760 --> 00:03:27,800 We'll look at how to fix that in the next video. See you then.