iOS 【Multithreading-NSOperation引入/NSInvocationOperation(了解/不常用)】

/**
 *  NSOperation 基于GCD
 *  底层是GCD,但是比GCD多了一些更简单使用的功能。更加面向对象
 *  是基于OC的,线程声明周期自动管理,经常使用
 */

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSLog(@"%@",[NSThread currentThread]);
    
    // 创建队列对象
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    // 创建操作对象
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download)
 object:nil];
    
    // 直接调用start,默认同步执行(在当前线程执行操作)
//    [operation start];
    
    // 添加操作对象到队列中,会自动异步执行
    [queue addOperation:operation];
}

- (void)download
{
    NSLog(@"-----download-----");
    NSLog(@"%@",[NSThread currentThread]);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

你可能感兴趣的:(ios,multithreading)