iOS 有三种多线程编程技术:
它们的抽象程度由低到高,越高的使用起来越简单.
+ (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)argument;
- (instancetype)initWithTarget:(id)target selector:(SEL)selector object:(id)argument
[thread start]
- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait
+ (NSThread *)currentThread; //获得当前线程
+ (void)sleepForTimeInterval:(NSTimeInterval)ti; //线程休眠
+ (NSThread *)mainThread; //主线程,即 UI线程
+ (BOOL)isMainThread; //判断是否主线程
- (BOOL)isMainThread; //同上
- (BOOL)isExecuting; // 线程是否正在运行
- (BOOL)isFinished; // 线程是否已结束
Demo:
创建一个 lable, 实现程序运行起来一段时间后就能显示一些内容,并且有一个 button 的按钮,当点击时,模拟从网络上刷新出一张图片,从中我们要了解到当需要更新 UI 时,必须在主线程中进行.从用户的角度来说,我看一张图片,不能影响的我在界面的其他操作,否则,这个 app 用着体验太差,我可能会删掉不再使用.
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *lable;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1.类方法
[NSThread detachNewThreadSelector:@selector(dosomething:) toTarget:self withObject:@"detachNewThreadSelector"];
}
- (void)dosomething:(NSString * )string
{
// 休眠5秒钟,只是模拟一个耗时的操作,如果操作耗时,就另起一个线程执行
[NSThread sleepForTimeInterval:5];
//更新 UI, 必须在主线程中进行 //NO 异步 YES 同步
[self performSelectorOnMainThread:@selector(updateLable:) withObject:string waitUntilDone:NO];
NSLog(@"%s",__func__);
}
- (void)updateLable:(NSString*)string
{
_lable.text = string;
}
#pragma mark - image view
- (IBAction)btnAction:(UIButton *)sender {
//实例方法
NSString *urlStr = @"http://img.hb.aicdn.com/5a8f57157b47284724d09ffd2da28369731f8144ac9c-1XdZKJ_fw658";
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(fetchImage:) object:urlStr];
thread.name = @"lisi";
[thread start];
}
- (void)fetchImage:(NSString*)urlStr
{
// 进行耗时的网络请求
[NSThread sleepForTimeInterval:4];
NSURL *url = [NSURL URLWithString:urlStr];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
if ([[NSThread currentThread].name isEqualToString:@"lisi"]) {
BOOL isMainThread = [[NSThread currentThread] isMainThread];
NSLog(@">>>>>isMainThread:%d",isMainThread);
}else{
BOOL isMainThread = [[NSThread currentThread] isMainThread];
NSLog(@"<<<<<<isMainThread:%d",isMainThread);
}
[self performSelectorOnMainThread:@selector(updateImageView:) withObject:image waitUntilDone:YES];
}
- (void)updateImageView:(UIImage *)image
{
if ([[NSThread currentThread] isMainThread]) {
NSLog(@"主线程:%s",__func__);
}
_imageView.image = image;
}
运行结果:
2015-08-02 14:49:31.823 03-NSThreadDemo[3151:781147] -[ViewController dosomething:]
2015-08-02 14:49:34.539 03-NSThreadDemo[3151:781778] >>>>>isMainThread:0
2015-08-02 14:49:34.539 03-NSThreadDemo[3151:780917] 主线程:-[ViewController updateImageView:]
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/www_nyp_boke/article/details/47207471