#import "ViewController.h"@interfaceViewcontroller () @property (weak, nonatomic) Iboutlet UILabel*Remindlabel;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}-(Ibaction) Sendbtnclick: (UIButton *) Sender {/*--First use GCD to send requests and refresh ui--*/Dispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{sleep (2); Dispatch_async (Dispatch_get_main_queue (),^{_remindlabel.text=@"sent successfully"; }); }); /*--The second uses nsblockoperation, Nsoperationqueue queue to send requests and refresh ui--*/Nsoperationqueue*myqueue=[[Nsoperationqueue alloc]init]; Nsblockoperation*operation=[nsblockoperation blockoperationwithblock:^{ //2 seconds DelaySleep2); //main thread Refresh UIDispatch_async (Dispatch_get_main_queue (), ^{_remindlabel.text=@"sent successfully"; }); }]; [Myqueue addoperation:operation]; /*--The third uses Nsthread to send requests and refresh ui--*/[Nsthread detachnewthreadselector: @selector (sendstate) totarget:self Withobject:nil]; }-(void) sendstate{Sleep (2); Dispatch_async (Dispatch_get_main_queue (),^{[Self updatelabel]; });}-(void) updatelabel{_remindlabel.text=@"sent successfully";}@end
Three ways to create multithreading through GCD, nsoperationqueue queues, Nsthread