GCD for iOS development and gcd for ios development

Source: Internet
Author: User

GCD for iOS development and gcd for ios development
Use GCD for iOS development (1) Use GCD to create an asynchronous task

# Pragma mark-use GCD to create an asynchronous task-(void) createAsyncTask {// create an asynchronous task // parameter 1: Pass in queue, there are 3 types of queue // main queue columns (UI main thread) // global queue (understood as working thread) // custom queue dispatch_queue_t queue = dispatch_get_global_queue (queue, 0); dispatch_async (queue, ^ {for (int I = 0; I <20; I ++) {NSLog (@ "A = % d ", i) ;}}); dispatch_async (queue, ^ {for (int I = 0; I <20; I ++) {NSLog (@ "B = % d ", I );}});}

 

(2) Simulated network download
# Pragma mark-simulated network download-(void) simulateNetwordDownload {_ progressView = [[UIProgressView alloc] initWithFrame: CGRectMake (10,100,300, 20)]; [self. view addSubview: _ progressView]; // dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {for (int I = 0; I <100; I ++) {// The UI cannot be directly updated in the Child thread // progressView. progress + = 0.01; // (1) UI update dispatch_get_main_queue (): switch back to the main thread to update UI dispatch_async (dispatch_get_main_queue (), ^ {_ progressView. progress + = 0.01;}); [NSThread sleepForTimeInterval: 0.1];} // (2) the last displayed dialog box dispatch_async (dispatch_get_main_queue (), ^ {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "prompt" message: @ "Download complete" delegate: self cancelButtonTitle: @ "cancel" otherButtonTitles: nil]; [alert show] ;}); // (1) (2) Synchronous execution, first (1) and then (2 )});}

 

(3) execute only once to implement Singleton (recommended implementation mode, thread security)
# Pragma mark-run only once to implement Singleton (recommended method, thread security)-(void) runOnce {static dispatch_once_t onceToken; dispatch_once (& onceToken, ^ {NSLog (@ "code executed only once ");});}

 

(4) delayed execution
# Pragma mark-delayed execution-(void) delayRun {dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (5 * NSEC_PER_SEC), dispatch_get_main_queue (), ^ {NSLog (@ "1111111 ");});}

 

(5) The notification executes multiple tasks asynchronously at the same time and waits for all tasks to be downloaded for processing (similar to thunder)
# Pragma mark-notifies you to execute multiple tasks and wait for all tasks to be downloaded for processing (similar to thunder)-(void) groupRun {// group Task group dispatch_group_t group = dispatch_group_create (); // complete dispatch_group_async (group, dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) in 7 s, ^ {for (int I = 0; I <100; I ++) {NSLog (@ "A = % d", I); [NSThread sleepForTimeInterval: 0.07] ;}}); // 5s finish dispatch_group_async (group, dispatch_get_global_queue (queue, 0 ), ^ {for (int I = 0; I <100; I ++) {NSLog (@ "B = % d", I); [NSThread sleepForTimeInterval: 0.05] ;}}); // 10 s complete dispatch_group_async (group, dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {for (int I = 0; I <100; I ++) {NSLog (@ "C = % d", I); [NSThread sleepForTimeInterval: 0.1] ;}}); dispatch_group_notify (group, dispatch_get_global_queue (queue, 0 ), ^ {NSLog (@ "all tasks are completed, automatic shutdown ");});}

 

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.