iOS NSThread和NSOperation的基本使用詳解
NSThread適合簡單的耗時(shí)任務(wù)的執(zhí)行,它有兩種執(zhí)行方法
- (void)oneClick{ [NSThread detachNewThreadSelector:@selector(doSomething:) toTarget:self withObject:@"oneClick"]; } -(void)doSomething:(NSString*) str{ NSLog(@"%@",str); } - (void)twoClick{ NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(doSomething:) object:@"twoClick"]; [myThread start]; }
NSOperation適合需要復(fù)雜的線程調(diào)度的方法,然后它默認(rèn)是使用主線程不會(huì)創(chuàng)建子線程
- (void)threeClick{ // 1.創(chuàng)建NSInvocationOperation對(duì)象 NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil]; // 2.調(diào)用start方法開始執(zhí)行操作 [op start]; } - (void)run { NSLog(@"------%@", [NSThread currentThread]); } - (void)fourClick{ NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{ // 在主線程 NSLog(@"1------%@", [NSThread currentThread]); }]; // 添加額外的任務(wù)(在子線程執(zhí)行) [op addExecutionBlock:^{ NSLog(@"2------%@", [NSThread currentThread]); }]; [op addExecutionBlock:^{ NSLog(@"3------%@", [NSThread currentThread]); }]; [op addExecutionBlock:^{ NSLog(@"4------%@", [NSThread currentThread]); }]; [op start]; }
以上這篇iOS NSThread和NSOperation的基本使用詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
iOS開發(fā)之獲取系統(tǒng)相冊(cè)中的圖片與視頻教程(內(nèi)帶url轉(zhuǎn)換)
本篇文章主要介紹了iOS開發(fā)之獲取系統(tǒng)相冊(cè)中的圖片與視頻教程(內(nèi)帶url轉(zhuǎn)換),主要介紹AssetsLibrary 框架,具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11IOS UITableView顏色設(shè)置的實(shí)例詳解
這篇文章主要介紹了IOS UITableView顏色設(shè)置的實(shí)例詳解的相關(guān)資料,這里提供了幾種方法幫助大家掌握這部分內(nèi)容,需要的朋友可以參考下2017-08-08iOS實(shí)現(xiàn)左右可滑動(dòng)的選擇條實(shí)例代碼分享
本文通過實(shí)例代碼給大家介紹了ios實(shí)現(xiàn)左右可滑動(dòng)的選擇條功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-03-03iOS 請(qǐng)求權(quán)限封裝類的實(shí)例代碼
下面小編就為大家分享一篇iOS 請(qǐng)求權(quán)限封裝類的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01