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];}