iOS NSThread和NSOperation的基本使用詳解
NSThread適合簡(jiǎn)單的耗時(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方法開(kāi)始執(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的基本使用詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
你知道Tab Bar圖標(biāo)原來(lái)還可以這樣玩嗎
這篇文章主要給大家介紹了關(guān)于Tab Bar圖標(biāo)另外一些大家不知道的玩法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
iOS中從網(wǎng)絡(luò)獲取數(shù)據(jù)的幾種方法的比較
IOS中獲取網(wǎng)絡(luò)數(shù)據(jù)一般有三種:1、NSURLCondition(已過(guò)時(shí)) 2、NSURLSession 3、三方庫(kù)AFNetWorking。下面通過(guò)本文給大家比較這三種方法的區(qū)別對(duì)比2017-11-11
舉例講解iOS開(kāi)發(fā)中拖動(dòng)視圖的實(shí)現(xiàn)
這篇文章主要介紹了舉例講解iOS開(kāi)發(fā)中的拖動(dòng)視圖實(shí)現(xiàn),代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-10-10
iOS基礎(chǔ)知識(shí)之@property 和 Ivar 的區(qū)別
這篇文章主要介紹了iOS基礎(chǔ)知識(shí)之@property 和 Ivar 的區(qū)別介紹,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08

