線程間通訊
一、NSThread
1.簡(jiǎn)單說明
①線程間通信:在1個(gè)進(jìn)程中,線程往往不是孤立存在的,多個(gè)線程之間需要經(jīng)常進(jìn)行通信
②線程間通信的體現(xiàn)
1個(gè)線程傳遞數(shù)據(jù)給另1個(gè)線程
在1個(gè)線程中執(zhí)行完特定任務(wù)后,轉(zhuǎn)到另1個(gè)線程繼續(xù)執(zhí)行任務(wù)
③線程間通信常用方法
// waitUntilDone的含義://? ? 如果傳入的是YES: 那么會(huì)等到主線程中的方法執(zhí)行完畢, 才會(huì)繼續(xù)執(zhí)行下面其他行的代碼//? ? 如果傳入的是NO: 那么不用等到主線程中的方法執(zhí)行完畢, 就可以繼續(xù)執(zhí)行下面其他行的低嗎/* *? 回到主線程執(zhí)行,執(zhí)行self的showImage方法,參數(shù)是image */[self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:YES];/* *? 回到xx線程執(zhí)行aSelector方法,參數(shù)是arg */- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait;
④案例,下載圖片,然后在屏幕上顯示
注意:雖然有時(shí)候可以在子線程中操作UI,但是開發(fā)中千萬不要這樣干因?yàn)槿绻窃谧泳€程中操作UI, 有時(shí)候行, 有時(shí)候不行
- (void)viewDidLoad{// 1.給定圖片的urlNSURL *url = [NSURL URLWithString:@"http://b.hiphotos.baidu.com/image/pic/item/e4dde71190ef76c666af095f9e16fdfaaf516741.jpg"];// 2.開啟線程,在后臺(tái)執(zhí)行download方法? [self performSelectorInBackground:@selector(download:) withObject:url];}- (void)download:(NSURL *)url{// 在子線程下載圖片NSData *data = [NSData dataWithContentsOfURL:url];UIImage *image = [UIImage imageWithData:data];// 設(shè)置圖片,執(zhí)行self.imageView的setImage:方法//? ? [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];// 另一張?jiān)O(shè)置圖片的方法// 回到主線程中執(zhí)行 showImage:方法,在此方法中設(shè)置圖片? ? [self performSelector:@selector(showImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES];}-(void)showImage:(UIImage *)image{// 更新UIself.imageView.image = image;
}
幾個(gè)常用打印時(shí)間的方法
//獲得當(dāng)前時(shí)間,double型? ? CFAbsoluteTime begin = CFAbsoluteTimeGetCurrent();// 獲得當(dāng)前時(shí)間NSDate *begin = [NSDate date];// 執(zhí)行一些操作之后的時(shí)間NSDate *end = [NSDate date];// 時(shí)差NSLog(@"花費(fèi)了多少秒 %f", [end timeIntervalSinceDate:begin]);
二、GCD
案例,下載圖片,然后在屏幕上顯示
在dispatch_get_main_queue() 隊(duì)列中
如果是通過異步函數(shù)調(diào)用, 那么會(huì)先執(zhí)行完所有的代碼, 再更新UI
如果是同步函數(shù)調(diào)用, 那么會(huì)先更新UI, 再執(zhí)行其它代碼
dispatch_queue_t queue = dispatch_get_global_queue(0,0);// 1.下載圖片(耗時(shí))dispatch_async(queue, ^{NSLog(@"%@", [NSThread currentThread]);// 1.創(chuàng)建URLNSURL *url = [NSURL? URLWithString:@"http://stimgcn1.s-msn.com/msnportal/ent/2015/08/04/7a59dbe7-3c18-4fae-bb56-305dab5e6951.jpg"];// 2.通過NSData下載圖片NSData *data = [NSData dataWithContentsOfURL:url];// 3.將NSData轉(zhuǎn)換為圖片UIImage *image = [UIImage imageWithData:data];// 4.更新UI//? ? ? ? self.imageView.image = image;//? ? ? ? NSLog(@"更新UI完畢");// 如果是通過異步函數(shù)調(diào)用, 那么會(huì)先執(zhí)行完所有的代碼, 再更新UI// 如果是同步函數(shù)調(diào)用, 那么會(huì)先更新UI, 再執(zhí)行其它代碼dispatch_sync(dispatch_get_main_queue(), ^{NSLog(@"%@", [NSThread currentThread]);self.imageView.image = image;NSLog(@"更新UI完畢");? ? ? ? });NSLog(@"Other");
});
三、NSOperation
1.第一種方法
// 1.創(chuàng)建一個(gè)新的隊(duì)列? ? NSOperationQueue *queue = [[NSOperationQueue alloc] init];// 2.添加任務(wù)(操作)? ? [queue addOperationWithBlock:^{// 2.1在子線程中下載圖片NSURL *url? = [NSURL URLWithString:@"http://imgcache.mysodao.com/img2/M04/8C/74/CgAPDk9dyjvS1AanAAJPpRypnFA573_700x0x1.JPG"];NSData *data = [NSData dataWithContentsOfURL:url];UIImage *image = [UIImage imageWithData:data];// 2.2回到主線程更新UI? ? ? ? [[NSOperationQueue mainQueue] addOperationWithBlock:^{self.imageView.image = image;
}];
}];
2.第二種方法(添加依賴)
/1.創(chuàng)建一個(gè)隊(duì)列// 一般情況下, 在做企業(yè)開發(fā)時(shí)候, 都會(huì)定義一個(gè)全局的自定義隊(duì)列, 便于使用? ? NSOperationQueue *queue = [[NSOperationQueue alloc] init];// 2.添加一個(gè)操作下載第一張圖片? ? __blockUIImage *image1 =nil;? ? NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{NSURL *url? = [NSURL URLWithString:@"http://imgcache.mysodao.com/img2/M04/8C/74/CgAPDk9dyjvS1AanAAJPpRypnFA573_700x0x1.JPG"];NSData *data = [NSData dataWithContentsOfURL:url];? ? ? ? image1 = [UIImage imageWithData:data];? ? }];// 3.添加一個(gè)操作下載第二張圖片? ? __blockUIImage *image2 =nil;? ? NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{NSURL *url? = [NSURL URLWithString:@"http://imgcache.mysodao.com/img1/M02/EE/B5/CgAPDE-kEtqjE8CWAAg9m-Zz4qo025-22365300.JPG"];NSData *data = [NSData dataWithContentsOfURL:url];? ? ? ? image2 = [UIImage imageWithData:data];? ? }];// 4.添加一個(gè)操作合成圖片? ? NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{? ? ? ? UIGraphicsBeginImageContext(CGSizeMake(200,200));? ? ? ? [image1 drawInRect:CGRectMake(0,0,100,200)];? ? ? ? [image2 drawInRect:CGRectMake(100,0,100,200)];UIImage *res = UIGraphicsGetImageFromCurrentImageContext();? ? ? ? UIGraphicsEndImageContext();// 5.回到主線程更新UI? ? ? ? [[NSOperationQueue mainQueue] addOperationWithBlock:^{self.imageView.image = res;? ? ? ? }];? ? }];// 6.添加依賴? ? [op3 addDependency:op1];? ? [op3 addDependency:op2];// 7.添加操作到隊(duì)列中
[queue addOperation:op1];
[queue addOperation:op2];
[queue addOperation:op3];