码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 多线程技术1

时间:2015-08-02 16:55:20      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:显示   nsthread   实例   ios   

iOS 多线程技术1


iOS 有三种多线程编程技术:

  • NSThread
  • NSOperation
  • GCD

它们的抽象程度由低到高,越高的使用起来越简单.

NSThread


显示调用 NSthread 类


  • 类方法
+ (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
  • 在指定的线程中执行,但是该线程必须具备 run loop
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait

常见NSThread 的方法


+ (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:]

技术分享


版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS 多线程技术1

标签:显示   nsthread   实例   ios   

原文地址:http://blog.csdn.net/www_nyp_boke/article/details/47207471

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!