IOS Multithreading Technology 1
IOS has three types of multithreaded programming techniques:
They are abstracted from low to high, and the higher it is, the simpler it is to use.
Nsthread Display Call Nsthread class
+ (void)detachNewThreadSelector:(SEL)selectortoTarget:(id)targetwithObject:(id)argument;
- (instancetype)initWithTarget:(id)target selector:(SEL)selector object:(id)argument [thread start]
implicit invocation
- Turn on background threads
- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg
- Running in the main thread
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait
- Executes in the specified thread, but the thread must have a run loop
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait
Common methods of Nsthread
+ (nsthread *) CurrentThread; //get current thread + (void ) Sleepfortimeinterval: (nstimeinterval ) ti; //thread hibernation + (nsthread *) Mainthread; //main thread, i.e. UI thread + (bool ) Ismainthread; //determine if the main thread -(bool ) Ismainthread; //ibid. -(bool ) isexecuting; //whether the thread is running -(bool ) isfinished; //thread has ended
Demo:
Create a lable, implement the program after a period of time to display some content, and there is a button buttons, when clicked, the simulation from the network to refresh a picture, we have to understand that when the UI needs to be updated, must be in the main thread. From the user's point of view, I look at a picture, Can not affect my other operations in the interface, otherwise, this app with experience is too poor, I may be deleted no longer use.
@interface viewcontroller ()@property(Weak,nonatomic)Iboutlet Uiimageview*imageview;@property(Weak,nonatomic)Iboutlet UILabel*lable;@end @implementation viewcontroller - (void) Viewdidload {[SuperViewdidload];//1. Class method[NsthreadDetachnewthreadselector:@selector(dosomething:) Totarget: Selfwithobject:@"Detachnewthreadselector"];} - (void) DoSomething: (NSString*) string{//Hibernate for 5 seconds, just simulate a time-consuming operation, if the operation is time consuming, another thread executes[NsthreadSleepfortimeinterval:5];//Update UI,//no asynchronous YES synchronization must be performed in the main thread[ SelfPerformselectoronmainthread:@selector(updatelable:) withobject:string Waituntildone:NO];NSLog(@'%s ', __func__);} - (void) Updatelable: (NSString*) string{_lable. Text= string;}#pragma mark-image View- (ibaction) Btnaction: (UIButton*) Sender {//Instance method NSString*URLSTR = @"http://img.hb.aicdn.com/5a8f57157b47284724d09ffd2da28369731f8144ac9c-1XdZKJ_fw658";Nsthread*thread = [[NsthreadAlloc] Initwithtarget: SelfSelector@selector(fetchimage:) object:urlstr]; Thread. Name= @"Lisi"; [Thread start];} - (void) Fetchimage: (NSString*) urlstr{//Make time-consuming network requests[NsthreadSleepfortimeinterval:4];Nsurl*url = [NsurlURLWITHSTRING:URLSTR]; NSData *data = [NSData Datawithcontentsofurl:url];UIImage*image = [UIImageImagewithdata:data];if([[NsthreadCurrentThread]. Nameisequaltostring:@"Lisi"]) {BOOLIsmainthread = [[NsthreadCurrentThread] Ismainthread];NSLog(@">>>>>ismainthread:%d", Ismainthread); }Else{BOOLIsmainthread = [[NsthreadCurrentThread] Ismainthread];NSLog(@"<<<<<<ismainthread:%d", Ismainthread); } [ SelfPerformselectoronmainthread:@selector(Updateimageview:) withobject:image Waituntildone:YES];} - (void) Updateimageview: (UIImage*) image{if([[NsthreadCurrentThread] Ismainthread]) {NSLog(@"Main thread:%s", __func__); } _imageview. Image= Image;}
Operation Result:
2015-08-02 14:49:31.823 03-nsthreaddemo[3151:781147]-[viewcontroller dosomething:]
2015-08-02 14:49:34.539 03-nsthreaddemo[3151:781778] >>>>>ismainthread:0
2015-08-02 14:49:34.539 03-nsthreaddemo[3151:780917] Main thread:-[viewcontroller Updateimageview:]
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
IOS Multithreading Technology 1