GCD understanding of Multithreading in iOS development

Source: Internet
Author: User
Tags gcd

GCD is the abbreviation for Grand Central Dispatch. Grand Central Dispatch (GCD) is a relatively new solution for multi-core programming developed by Apple.  It was first introduced in Mac OS X 10.6 Snow Leopard and was recently introduced into the iOS4.0. GCD is an efficient and powerful technology that replaces technologies such as Nsthread. GCD can handle complex asynchronous programming issues such as data locking and resource leaks.

GCD can do a lot of things, but this is just about the basics of implementing multithreading in iOS apps.  Before you begin, it is necessary to understand that the code block to be provided to the GCD queue is used to schedule the run on a system or user-created queue. declaring a queue

A user-created queue is returned as follows:

dispatch_queue_t myqueue = dispatch_queue_create ("Com.iphonedevblog.post", NULL), where the first parameter is the identity queue, The second parameter is the parameter used to define the queue (not currently supported, so pass in null).

Execute a queue

 The incoming code is executed asynchronously as follows:

Dispatch_async (Myqueue, ^{[self dosomething];}); Where you first pass in a previously created queue, and then provide a block of code that is run by the queue.

Declaring and executing a queue

If you do not need to keep a reference to the queue you want to run, you can implement the previous functionality by using the following code: Dispatch_async (Dispatch_queue_create ("Com.iphonedevblog.post", NULL), ^{[self DoSomething]; });    If you need to pause a queue, you can call the following code.  Pausing a queue prevents all code associated with that queue from running. Dispatch_suspend (Myqueue); pausing a queue 

If you pause a queue, do not forget to recover. Pause and resume operations are similar to retain and release in memory management. Calling Dispatch_suspend increases the pause count, while the dispatch_resume decreases. The queue starts running only if the pause count becomes zero. Dispatch_resume (Myqueue); Recovering a queue running code from the queue in the main thread some operations cannot run in an asynchronous queue, so you must run on the main thread (one for each application). The UI drawing and any calls to Nsnotificationcenter must be made on the mainline stroke. An example of accessing the main thread and running code in another queue is as follows: Dispatch_sync (Dispatch_get_main_queue (), ^{[self Dismissloginwindow];}); Note that dispatch_suspend (and dispatch_resume) do not work on the main thread.  

With GCD, you can make your program not lose its response. Multithreading is not easy to use, with the use of GCD, it will make it easier. You don't need to specialize in thread management, it's great!

The principle of keeping your program responsive:

1. Do not plunger the main thread

2. Take the work one to the other threads.

3. Update the UI of the main thread when done.

To illustrate:

No GCD code:

-(void) Addtweetwithmsg: (nsstring*) msg URL: (nsurl*) URL {

Called in the main thread.

Dtweet *TW = [[Dtweet alloc] initwithmsg:msg];

[Tweets addtweet:tw Display:yes];

tw.img = [Imagecache getimgfromurl:url];//bottle neck

[Tweets updatetweet:tw Display:yes];

[TW release];

}

Code with GCD:

-(void) Addtweetwithmsg: (nsstring*) msg URL: (nsurl*) URL {

//In the main thread call.

Dtweet *TW = [[Dtweet alloc] initwithmsg:msg];

[Tweets addtweet:tw Display:yes];

Dispatch_async (Image_queue, ^{

tw.img = [Imagecache getimgfromurl:url];//is placed in an asynchronous queue.

Dispatch_async (Main_queue, ^{

[Tweets updatetweet:tw display:yes];//is placed in the main thread of the async.

});

});

[TW release];

}

1. GCD is part of Libsystem.dylib

2. #include <dispatch/dispatch.h>

a Nsthread method: The code is as follows:

-(void) viewdidload

{

[Super viewdidload];

Nsthread *thread1=[[nsthread alloc]initwithtarget:self selector: @selector (print1) Object:nil];

[Thread1 start];

nsthread *thread2=[[nsthread alloc]initwithtarget:self selector: @selector (Print2) Object:nil];

[Thread2 start];

}

-(void) print1{

for (int i=0; i<100; i++) {

NSLog (@ "I am print1 is executing%d", i);

}

}

-(void) print2{

for (int i=0; i<100; i++) {

NSLog (@ "Print2 is executing%d", i);

}

}

two "

Nsinvocationoperation

method: The code is as follows

Nsinvocationoperation *operation1=[[nsinvocationoperation alloc]initwithtarget:self selector: @selector (PRINT1) object:@ "1"];

Nsinvocationoperation *operation2=[[nsinvocationoperation alloc]initwithtarget:self selector: @selector (Print2) object:@ "2"];//of course there is a way to do this.

Nsoperationqueue *queue=[[nsoperationqueue Alloc]init];

[Queue Addoperation:operation1];

[Queue Addoperation:operation2];

three "

GCD

method: The code is as follows

dispatch_queue_t t1=dispatch_queue_create ("1", NULL);

dispatch_queue_t t2=dispatch_queue_create ("2", NULL);

dispatch_async (t1, ^{

[Self print1];

});

Dispatch_async (T2, ^{

[Self print2];

});

http://www.cnblogs.com/pengyingh/articles/2356825.htmlhttp://www.cnblogs.com/vinceoniphone/archive/2011/04/07/ 2007968.html//Detailed address. HTTP://BLOG.CSDN.NET/ZHUQILIN0/ARTICLE/DETAILS/6527113//message push mechanism

The principle of push:

The working mechanism of Push can be summarized simply as

In the figure, provider is the push server for an iphone software, which I will use. NET as the provider. APNS is an abbreviation for Apple push Notification Service (Apple push server) and is Apple's server.

Can be divided into three stages.

Phase one:. NET applications package the message to be sent, the identity of the target iphone, and send it to APNs.  Phase II: APNs in its own list of iphone's registered push services, look for an iphone with a corresponding logo and send the message to the iphone. The third stage: The iphone sends the message to the appropriate application and follows the settings to eject the push notification.

HTTP://BLOG.CSDN.NET/ZHUQILIN0/ARTICLE/DETAILS/6527113//message push mechanism to see when memory leaks: Search for Run to find run Static Snalyzer.
    • Gcd1.zip (23.1 KB)

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.