Multithreading in IOS development

Source: Internet
Author: User

Multithreading in IOS development
I. Five ways to create multithreading 1. method 1: NSThread * thread = [[NSThread alloc] initWithTarget: self selector: @ selector (_ update) object: nil]; 2. method 2 for enabling a thread [NSThread detachNewThreadSelector: @ selector (_ update) toTarget: self withObject: nil]; 3. method 3 of enabling a thread [self Checking mselectorinbackground: @ selector (_ update) withObject: nil]; 4. method 4 NSOperationQueue * queue = [[NSOperationQueue alloc] init]; [queue addOperationWithBlock: ^ {for (int I = 0; I <50; I ++) {printf ("subthread \ n") ;}}]; 5. method 5 of enabling a thread // Step 1 enable the thread pool NSOperationQueue * queue = [[NSOperationQueue alloc] init]; // set the number of concurrent threads [queue setMaxConcurrentOperationCount: 2]; // create multiple threads and add them to the thread pool NSInvocationOperation * thread1 = [[NSInvocationOperation alloc] initWithTarget: self selector: @ selector (_ update1) object: nil]; NSInvocationOperation * thread2 = [[NSInvocationOperation alloc] initWithTarget: self selecto R: @ selector (_ update2) object: nil]; [thread1 setQueuePriority: Permission]; [thread2 setQueuePriority: Permission]; [queue addOperation: thread1]; [queue addOperation: thread2]; 2. multi-threaded application instance, loading images. 1. The core idea is to consider the delay of loading network images. Loading in a main thread will affect the rendering of controls. In this case, multithreading can be adopted. After asynchronous loading, the UI is refreshed. 2. implement multi-threaded download by adding categories for UIImageView. Main Code: # import "UIImageView + thread. h "@ implementation UIImageView (load)-(void) setImageWithUrl :( NSString *) url {[self received mselectorinbackground: @ selector (_ loadImage :) withObject: url];}-(void) _ loadImage :( NSString *) u {@ autoreleasepool {NSURL * url = [NSURL URLWithString: u]; NSData * data = [NSData dataWithContentsOfURL: url]; UIImage * image = [UIImage imageWithData: data]; [self defined mselecw.mainthread: @ selector (setImage :) withObject: image waitUntilDone: NO];}

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.