Gcd ios Summary

Source: Internet
Author: User

There are two ways to assign tasks to the main queue, both of which are asynchronous, even if the task is not executed

Program continues:

The dispatch_async function executes a block object on the dispatch queue.

The dispatch_async_f function executes a C function on the dispatch queue.

 

1. The dispatch_async function executes a block object in the dispatch queue.

The dispatch_sync method cannot be called in the main queue, because the thread will be blocked indefinitely and your application will be locked. All tasks submitted to the main queue through GCD must be submitted asynchronously.

Dispatch_queue_t mainqueue = dispatch_get_main_queue ();

Dispatch_async (mainqueue, ^ {

[[Uialertview alloc] initwithtitle: @ "GCD"

Message: @ "GCD is amazing! "

Delegate: Nil cancelbuttontitle: @ "OK"

Otherbuttontitles: nil, nil] Show];

});

2. The dispatch_async_f function executes a C function on the dispatch queue.

The first parameter of the dispatch_get_global_queue function describes the priority of the concurrent queue. This attribute is GCD.

It must be retrieved for programmers. The higher the priority, more CPU timeslice will be provided to obtain the code executed by the queue.

You can use the following values as the first parameter of the dispatch_get_global_queue function: dispatch_queue_priority_low

Your Tasks use less timeslice than normal tasks.

Dispatch_queue_priority_default

The default system priority for code execution is applied to your tasks.

Dispatch_queue_priority_high

Compared with normal tasks, more timeslices will be applied to your tasks.

The second parameter of the dispatch_get_global_queue function has been saved. You only need to enter a value of 0 for it.

Dispatch_queue_t queue = dispatch_get_global_queue (dispatch_queue_priority_default, 0 );

Size_t numberofiteration = 10;

Dispatch_async (queue, ^ {

Dispatch_apply (numberofiteration, queue, ^ (size_t iteration ){

//

Nslog (@ "----- dispatch ");

});

});

III,

Non-UI tasks can be executed asynchronously with the help of GCD

Dispatch_queue_t currentqueue = dispatch_get_global_queue (dispatch_queue_priority_default, 0 );

Dispatch_async (currentqueue, ^ {

_ Block uiimage * image = nil;

Dispatch_sync (currentqueue, ^ {

// Download the image here

Nsstring * urlasstring = @ "http://images.apple.com/mobileme/features/images/ipad_findyouripad_20100518.jpg ";

Nsurlrequest * URLRequest = [nsurlrequest requestwithurl: [nsurl urlwithstring: urlasstring];

Nserror * downloaderror = nil;

Nsdata * imagedata = [nsurlconnection sendsynchronousrequest: URLRequest returningresponse: Nil error: & downloaderror];

If (downloaderror = nil & imagedata! = Nil ){

Image = [uiimage imagewithdata: imagedata];/* We have the image. We can use it now */

}

Else if (downloaderror! = Nil)

{

Nslog (@ "error happened = % @", downloaderror );

}

Else

{

Nslog (@ "no data cocould get downloaded from the URL .");

}

});

Dispatch_sync (dispatch_get_main_queue (), ^ {

// Show the image to the user here on the main queue

If (image! = Nil ){

/* Create the image view here */

Uiimageview * imageview = [[uiimageview alloc]

Initwithframe: Self. View. bounds];

/* Set the image */[imageview setimage: Image];

/* Make sure the image is not scaled incorrectly */

[Imageview setcontentmode: uiviewcontentmodescaleaspectfit];

/* Add the image to this view controller's view */[self. View addsubview: imageview];

} Else

{

Nslog (@ "image isn' t downloaded. Nothing to display .");

}

});

});

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.