Skip to main content

Day 40

About 2 minSwiftcrashcoursepaul-hudsonswifthacking-with-swiftxcodeappstore

Day 40 ๊ด€๋ จ


100 Days of Swift - Day 40

Project 9, part two

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
100 Days of Swift - Day 40 - 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.open in new window

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:

  1. 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!
  2. 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!
  3. 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!

์ด์ฐฌํฌ (MarkiiimarK)
Never Stop Learning.