Common multithreaded operations in iOS are (
Nsthread,
Nsopretion
CGD
)
In order to show more intuitive multi-threaded operation in SB to do the following interface layout:
Download images from the Web when you click on the download:
- (void) loadimgfromurl{Nsurl*url=[nsurl urlwithstring:@"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec =1456920204&di=dd5436b6925ec27e1a13243f425b3586&src=http://img.wallpapersking.com/d7/2012-8/2012081210373.jpg"]; NSData*data=[NSData Datawithcontentsofurl:url];//NSData *data1=[nsdata Datawithcontentsofurl:url];//NSData *data2=[nsdata datawithcontentsofurl:url]; Extended download Time//[Self performselectoronmainthread: @selector (upimgview:) withobject:data Waituntildone:yes];//updating the UI in the main thread[[Nsoperationqueue Mainqueue] Addoperationwithblock:^{[Self upimgview:data]; }];}
-(void) Upimgview: (NSData *) data{ self.imgView.image = [UIImage imagewithdata:data];}
-use Nsthread to implement asynchronous loading of pictures
1. Class Method:
[Nsthread detachnewthreadselector: @selector (Loadimgfromurl) totarget:self Withobject:nil];
2. Example method:
Nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (Loadimgfromurl) Object:nil];
[Thread start];
Two: Nsopreation
-Use nsopreation Subclass Nsinvocationoperation:
Nsinvocationoperation *invoca = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (loadimgfromurl ) Object:nil];
Nsoperationqueue *queue = [[Nsoperationqueue alloc] init];
The Nsinvocationoperation object is not called when it is created, but it is initiated by a Start method, but note that if you call the Start method directly, this operation is called in the main thread, which is not normally done. Instead, it's added to Nsoperationqueue.
[Invocationoperation start];
[Queue Addoperation:invoca];
2. Use the subclass of Nsopreation Nsblockoperation:
Nsoperationqueue *queue = [[Nsoperationqueue alloc] init];
Queue.maxconcurrentoperationcount =5;//Maximum number of concurrent
1 adding directly through the queue
[Queue addoperationwithblock:^{
[Self loadimgfromurl];
}];
2 Mr. Cheng Nsblockoperation Real example in add
Nsblockoperation *bop = [Nsblockoperation blockoperationwithblock:^{
[Self loadimgfromurl];
// }];
[Queue Addoperation:bop];
Three: GCD is most commonly used in iOS
GCD and Nsopreation are all operated in the form of queues. Just GCD is not object-oriented, it has two types of queue operations
1. Serial Queue 2. Parallel queue
As the name implies, serial operations are performed sequentially, while parallel queues are executed in parallel.
Line-hopping queue
dispatch_queue_t queue = dispatch_queue_create ("Myqueue", dispatch_queue_serial);
Parallel queues
dispatch_queue_t conqeue = dispatch_queue_create ("Myqueue", dispatch_queue_concurrent);
Dispatch_get_global_queue (Dispatch_queue_priority_high, 0);//Global Parallel queue
There are two types of gcd that are synchronous, one of which is asynchronous.
-Synchronization of
Dispatch_sync (Queue, ^{
[Self loadimgfromurl];
});
Dispatch_sync (Conqeue, ^{
[Self loadimgfromurl];
});
At this point, it can be found that the test button cannot be clicked at the time of synchronization execution, whether the queue is serial or parallel.
Asynchronous:
Dispatch_async (Queue, ^{
[Self loadimgfromurl];
});
The main thread is not blocked at this time;
Conclusion:
In GDC, whether an operation is multithreaded or single-threaded execution depends on the current queue type and execution method, only the queue type is a parallel queue and executes with an async method to execute in multiple threads.
Serial queues can be executed sequentially, and asynchronous methods of parallel queues cannot determine the order of execution.
Updating the UI interface is best done with synchronous methods, and other operations using asynchronous methods.
Easy use of multi-threading in IOS