Multithreading 0612
The only way to stop it from freezing is to use multithreading.
Multithreading means that we are going to have different threads of execution. Different places in our iPhone or iPad where code is running. These appear to run simultaneously, but it will even work on a single core processor, not a multiprocessor. It works by timesharing.
You got one thing running, another thing running and it’s kind of going back and forth. Letting them each run a little bit and maybe if one of them is more important than the other, it gets run a lot and the other one doesn’t get to run so much. But that’s basically what multithreading is all about.
Multithreading is mostly about “queue” in iOS. Functions (usually closures) are lined up in the queue. Queue meaning like, if you go to the movies maybe in the England, they would say, get in the queue, meaning the line get into the movie. So same thing here, and it’s the same thing as queue in the computer science sense.
A queue is a thing, a bunch of things lined up to do something. These queues contained an iOS functions. Most of the time these functions are closure. That you have put in there. Blocks of code that you put in this queue.
And then, the system simply runs along this queue, pulls the next thing off the queue and starts it running in a separate thread. And you can have mutiple of these queues and the system is pulling them off each one and running them, in their own threads, simple as that, you just got multithreading.
Multithreading
Queues
Multithreading is mostly about “queues” in iOS.
Functions are lined up in a queue, usually closures.
Then those functions are pulled off the queue and executed on an associated thread.
Queues can be serial, one at a time, or concurrent, multiple things going at once.
Main queue
It’s a very special serial queue. All UI activity must occur on this queue and this queue only. And conversely, non-UI activity that is at all time consuming must not occur on that queue. Because we want our UI to be highly responsive! And also because we want things that happen in the UI to happen predictablely, serially. Functions are pulled off and worked on in the main queue only when it is “quiet”.
Concurrent queues
Most non-main-queue work will happen on a concurrent queue with a certain quality of service.
- Interactive, quick and high priority
- Initiated, high priority, might take time
- Utility, long running
- Background, user not concerned with this
You will probably use these queues to do any work that you don’t want to block the main queue.