Concurrent introduction to IOS development (56)

Source: Internet
Author: User

1 Preface
Concurrency is everywhere in programming. How to better utilize concurrency in IOS development has become a key issue. Today we will briefly introduce concurrency.

2 Text

Concurrent Execution occurs when two or more tasks are executed simultaneously. Even if there is only one CPU, the modern operating system can execute multiple tasks at the same time.

Grand Central Dispatch, or GCD (Central scheduling), is a low-level c api that works with Block objects. The real purpose of GCD is to allocate tasks to multiple cores without worrying about which kernel to execute.

The core of GCD is the dispatch queue. Whether in iOS or Max OS X, we can see that the thread pool is managed by GCD in the main operating system. You will not directly have a working relationship with the thread. You only work on the dispatch queue, assign tasks to this queue, and request the queue to call your tasks. GCD provides several options for running tasks: Synchronous execution, asynchronous execution, and delayed execution.

To start using GCD in your APP, you do not have to import any special libraries to your project. Apple has incorporated various frameworks in GCD, including Core Foundation and Cocoa/Cocoa Touch. All methods and data types in GCD start with the dispatch _ keyword. For example, dispatch_async allows you to assign tasks in a queue for asynchronous execution, while dispatch_after allows you to run a block after a given delay. Traditionally, programmers must create their own threads to execute tasks in parallel.


We will always deal with the dispatch queue, so please make sure you fully understand the overview behind it

Read. There are three types of dispatch Queues:

Main Queue

This queue executes all its tasks on the main thread. Cocoa and Cocoa Touch allow programmers to call all the UI-related methods on the main thread. Use the dispatch_get_main_queue function to retrieve the handle of the main queue column.

Concurrent Queues


To execute asynchronous and synchronous tasks, you can retrieve the write queue in GCD. Multiple concurrent queues can be easily executed in parallel
These allow you to call a method in another existing object.


Tasks, no more thread management, cool! Use the dispatch_get_global_queue function to retrieve the handle of a concurrent queue.

Serial Queues

Whether you submit a synchronous or asynchronous task, these queues always execute the task according to the first-in-first-out (FIFO) principle, which means they execute a Block Object at a time. However, they do not run on the main thread, so it is a perfect choice for tasks that require strict execution in sequence and do not block the main thread. Use the dispatch_queue_create function to create a serial queue. Once you use the full queue, you must use the dispatch_release function to release it.

Block Object is a code package that usually appears in the form of methods in Objective-C. Block Objects and GCD jointly create a harmonious environment in which you can publish high-performance multi-thread APPs on iOS and Mac OS X. What's special about Block Objects and GCD? You may ask. Very simple: there are not many threads! All you need to do is put the code into Block Objects and ask GCD to execute the code carefully.

GCD works with Block Objects. When you use GCD to execute a task, you can pass a Block Object that can be executed synchronously or asynchronously, depending on the method you use in GCD. However, you can create a Block Object to download a URL that is passed to it as a parameter. A separate Block Object can be synchronously or asynchronously applied anywhere in the APP. This location depends on how you want to run it. You don't have to make the Block Object itself synchronous or asynchronous. You just need to call it through synchronous or asynchronous GCD methods, and it will work.

The operation can execute code blocks synchronously or asynchronously. You can manually manage operations or put them in the Operation queue, which will prompt

Concurrency makes it unnecessary for you to think about underlying thread management.

Cocoa provides three different operations:


Block Operations

They prompt execution of one or more Block Objects.

Invocation Operations

These allow you to call a method in another existing object.

Plain Operations

These are classes that need to be inherited from common operations. The code to be executed will be written into the main method of the operation object.

When the APP runs on iOS, the operating system creates at least one thread for the APP, called the main thread. Each thread must be added to a running loop. A running loop, as its name implies, is a process in which different events can occur, such as triggering a timer or running thread.

Imagine a running loop as a loop with a start point, a completion condition, and a series of events that occur within its lifecycle. A thread or timer is attached to a running loop. In fact, a running loop is required to activate its function.

The main thread of an APP is the thread that processes UI events. If you execute a long-running task in the main thread, you must note that the app ui does not respond or the response is slow. To avoid this, you can create an independent thread and/or timer to execute their respective tasks (even a long-running task) without blocking the main thread.

 


3 conclusion
The above is a brief introduction to concurrency, hoping to help you get started with understanding.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.