Day 40
Day 40 ๊ด๋ จ
Project 9, part two
Thereโs an old joke about multitasking:
A programmer has a problem and thinks, โI can fix this using multitasking!โ
have Now problems! two they
(Hey, I said it was old, I didnโt say it was funny!)
The point is that when you start running multiple pieces of code at the same time, they can complete in any order โ the โnow they have two problems!โ punchline got mashed up.
In fact, race conditions are a whole category of bugs caused by one task completing before it was supposed to โ they are particularly nasty to fix because sometimes work completes in the correct order and everything works great, which is why we call it a race.
Yesterday was a gentle introduction to multi-tasking using Grand Central Dispatch, but weโll be coming back to it more in the future. In the meantime, make sure you test what youโve learned so you can be sure itโs all sunk in.
Today you should work through the wrap up chapter for project 9, complete its review, then work through all three of its challenges.
Wrap up
Wrap up
Although I've tried to simplify things as much as possible, GCD still isn't easy. That said, it's much easier than the alternatives: GCD automatically handles thread creation and management, automatically balances based on available system resources, and automatically factors in Quality of Service to ensure your code runs as efficiently as possible. The alternative is doing all that yourself!
There's a lot more we could cover (not least how to create your own queues!) but really you have more than enough to be going on with, and certainly more than enough to complete the rest of this series. We'll be using GCD again, so it might help to keep this reference close to hand!
Review what you learned
Anyone can sit through a tutorial, but it takes actual work to remember what was taught. Itโs my job to make sure you take as much from these tutorials as possible, so Iโve prepared a short review to help you check your learning.
Click here to review what you learned in project 9.
Challenge
One of the best ways to learn is to write your own code as often as possible, so here are three ways you should try your new knowledge to make sure you fully understand whatโs going on:
- Modify project 1 so that loading the list of NSSL images from our bundle happens in the background. Make sure you call
reloadData()
on the table view once loading has finished! - Modify project 8 so that loading and parsing a level takes place in the background. Once youโre done, make sure you update the UI on the main thread!
- Modify project 7 so that your filtering code takes place in the background. This filtering code was added in one of the challenges for the project, so hopefully you didnโt skip it!
Wrap up - Additional