多線程--NSThread
main thread主線程
在一個(gè)運(yùn)行的iOS應(yīng)用中,處理UIKit對(duì)象的所有方法調(diào)用。
程序啟動(dòng)后,系統(tǒng)自動(dòng)創(chuàng)建主線程。
主線程阻塞,UI就會(huì)失去響應(yīng)。只能從主線程操作UI.
長(zhǎng)時(shí)間、大量計(jì)算及阻塞I/O都應(yīng)該另開線程。
NSThread的初始化
1.動(dòng)態(tài)
- (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument;
// 初始化線程
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
// 設(shè)置線程的優(yōu)先級(jí)(0.0 - 1.0,1.0最高級(jí))
thread.threadPriority = 1;
// 開啟線程
[thread start];
參數(shù):
selector:線程執(zhí)行的方法,最多只接收一個(gè)參數(shù)
target:selector消息發(fā)送的對(duì)象
argument:傳給selector的唯一參數(shù),也可以是nil
2.靜態(tài)
+ (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)argument;
[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
// 調(diào)用完畢后,會(huì)馬上創(chuàng)建并開啟新線程
3.隱式
[self performSelectorInBackground:@selector(run) withObject:nil];
獲取當(dāng)前線程
[self performSelectorInBackground:@selector(run) withObject:nil];
獲取主線程
NSThread *main = [NSThread mainThread];
暫停當(dāng)前線程
// 暫停2s
[NSThread sleepForTimeInterval:2];
// 或者
NSDate *date = [NSDate dateWithTimeInterval:2 sinceDate:[NSDate date]];
[NSThread sleepUntilDate:date];
線程間通信
1.在指定線程上執(zhí)行操作
[self performSelector:@selector(run) onThread:thread withObject:nil waitUntilDone:YES];
2.在主線程上執(zhí)行操作
[self performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:YES];
3.在當(dāng)前線程執(zhí)行操作
[self performSelector:@selector(run) withObject:nil];
多線程--Runloop
NSRunLoop是IOS消息機(jī)制的處理模式 。
主要作用:控制NSRunLoop里面線程的執(zhí)行和休眠,在有事情做的時(shí)候使當(dāng)前NSRunLoop控制的線程工作,沒有事情做讓當(dāng)前NSRunLoop的控制的線程休眠。
一直在循環(huán)檢測(cè),從線程start到線程end,檢測(cè)inputsource(如點(diǎn)擊,雙擊等操作)同步事件,檢測(cè)timesource同步事件,檢測(cè)到輸入源會(huì)執(zhí)行處理函數(shù),首先會(huì)產(chǎn)生通知,corefunction向線程添加runloop observers來監(jiān)聽事件,意在監(jiān)聽事件發(fā)生時(shí)來做處理。
runloopmode是一個(gè)集合,包括監(jiān)聽:事件源,定時(shí)器,以及需通知的runloop observers 模式包括:
default模式:幾乎包括所有輸入源(除NSConnection)
NSDefaultRunLoopMode模式 mode模式:處理modal panels
connection模式:處理NSConnection事件,屬于系統(tǒng)內(nèi)部,用戶基本不用
event tracking模式:如組件拖動(dòng)輸入源 UITrackingRunLoopModes 不處理定時(shí)事件
common modes模式:NSRunLoopCommonModes 這是一組可配置的通用模式。將input sources與該模式關(guān)聯(lián)則同時(shí)也將input sources與該組中的其它模式進(jìn)行了關(guān)聯(lián)。
多線程--NSOperation
作?:配合使用NSOperation和NSOperationQueue也能實(shí)現(xiàn)多線程編程。
NSOperation和NSOperationQueue實(shí)現(xiàn)多線程的具體步驟:
(1)先將需要執(zhí)行的操作封裝到一個(gè)NSOperation對(duì)象中
(2)然后將NSOperation對(duì)象添加到NSOperationQueue中
(3)系統(tǒng)會(huì)?動(dòng)將NSOperationQueue中的NSOperation取出來
(4)將取出的NSOperation封裝的操作放到?條新線程中執(zhí)?
NSOperation是個(gè)抽象類,并不具備封裝操作的能力,必須使?它的子類。
NSInvocationOperation:
NSInvocationOperation *operation=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test) object:nil];
[operation start];
NSBlockOperation:
//創(chuàng)建
NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{
}];
//添加
[operation addExecutionBlock:^{
}];
NSOperationQueue:
作?:NSOperation可以調(diào)?start?法來執(zhí)?任務(wù),但默認(rèn)是同步執(zhí)行的
NSOperationQueue * queue=[[NSOperationQueue alloc]init];
[queue addOperation:operation1];
[queue addOperationWithBlock:^{
}];
多線程--GCD
Grand Central Dispatch (GCD)是Apple開發(fā)的一個(gè)多核編程的解決方法。
dispatch queue分成以下三種:
1)運(yùn)行在主線程的Main queue,通過dispatch_get_main_queue獲取。
2)并行隊(duì)列g(shù)lobal dispatch queue,通過dispatch_get_global_queue獲取,由系統(tǒng)創(chuàng)建三個(gè)不同優(yōu)先級(jí)的dispatch queue。并行隊(duì)列的執(zhí)行順序與其加入隊(duì)列的順序相同。
3)串行隊(duì)列serial queues一般用于按順序同步訪問,可創(chuàng)建任意數(shù)量的串行隊(duì)列,各個(gè)串行隊(duì)列之間是并發(fā)的。
用法:
// 后臺(tái)執(zhí)行:
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// something
});
// 主線程執(zhí)行:
dispatch_async(dispatch_get_main_queue(), ^{
// something
});
// 一次性執(zhí)行:
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// code to be executed once
});
// 延遲2秒執(zhí)行:
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// code to be executed on the main queue after delay
});
// 自定義dispatch_queue_t
dispatch_queue_t urls_queue = dispatch_queue_create("blog.devtang.com", NULL);
dispatch_async(urls_queue, ^{
// your code
});
dispatch_release(urls_queue);
// 合并匯總結(jié)果
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
// 并行執(zhí)行的線程一
});
dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
// 并行執(zhí)行的線程二
});
dispatch_group_notify(group, dispatch_get_global_queue(0,0), ^{
// 匯總結(jié)果
});
加載網(wǎng)絡(luò)圖片出現(xiàn)的問題
NSURL *imgURL = [NSURL URLWithString:@"http://thumb.takefoto.cn/wp-content/uploads/2016/04/201604140840253233.jpg"];
錯(cuò)誤描述:
App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app‘s Info.plist file.
在iOS9 beta中,蘋果將原h(huán)ttp協(xié)議改成了https協(xié)議,使用 TLS1.2 SSL加密請(qǐng)求數(shù)據(jù)。
解決方法:
在info.plist 加入key
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
如圖:
