iOS:OC--NSOperation

  • 原理分析

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)invocationOperation:(id)sender {
    //創建invocation的對像
    NSInvocationOperation *invocation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(inBackground) object:nil];
    //需要手動開啟
    [invocation start];
}
-(void)inBackground
{
    NSLog(@"current = %@ main = %@",[NSThread currentThread],[NSThread mainThread]);
}

- (IBAction)blockOperation:(id)sender {
    NSBlockOperation *blockOper = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"current = %@ \n main = %@",[NSThread currentThread],[NSThread mainThread]);
    }];
    [blockOper start];
}
- (IBAction)operationQueue:(id)sender {
    //創建隊列對象:
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    NSInvocationOperation *invocation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(inBackground) object:nil];
    NSBlockOperation *block = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"current = %@ \n main = %@",[NSThread currentThread],[NSThread mainThread]);
    }];
    //將子類對象添加到隊列
    [queue addOperation:invocation];
    [queue addOperation:block];
    
//    [self performSelectorOnMainThread:<#(nonnull SEL)#> withObject:nil waitUntilDone:YES];
    //回到主線程的內容
    NSBlockOperation *onMain = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"current = %@ \n main = %@",[NSThread currentThread],[NSThread mainThread]);
        
    }];
    [[NSOperationQueue mainQueue]addOperation:onMain];
                                
}

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

@end
  • NSOperation實現添加圖片
#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong)UIImageView* imageView;
@property(nonatomic,strong)NSData* imageData;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.imageView = [[UIImageView alloc]initWithFrame:([UIScreen mainScreen].bounds)];
    [self.view addSubview:self.imageView];
}
#pragma mark --- 創建]子線程加載數據
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self performSelectorInBackground:@selector(loadImage) withObject:nil];
}
-(void)loadImage
{
    NSString *path = @"http://img4.imgtn.bdimg.com/it/u=2730485761,138060261&fm=26&gp=0.jpg";
    NSURL *url = [NSURL URLWithString:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    self.imageData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    //進入主線程,刷新 UI
    [self performSelectorOnMainThread:@selector(reloadImage) withObject:nil waitUntilDone:YES];
}
-(void)reloadImage
{
    NSLog(@"%@ \n %d",[NSThread currentThread],[NSThread isMainThread]);
    //imageView數據源展示
    self.imageView.image = [UIImage imageWithData:self.imageData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容