首页 > 代码库 > NSOperation使用的三种方法

NSOperation使用的三种方法

//1.invacationOperation

-(void)invocationOperation

{

    NSInvocationOperation *ip = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(operation ) object:nil];

    [ip start];

//    [self performSelector:@selector(operation) withObject:nil];

}

 

//2.blockOperation

-(void)blockOperation

{

    NSBlockOperation *bp = [NSBlockOperation blockOperationWithBlock:^{

//在主线程中完成

        NSLog(@"--1--%@",[NSThread currentThread]);

    }];

//额外任务在子线程完成

    [bp addExecutionBlock:^{

        NSLog(@"--2--%@",[NSThread currentThread]);

    }];

    [bp start];

    

 

//3.自定义PJXCustomOperation继承NSOperation

#import <Foundation/Foundation.h>

 

@interface PJXCustomOperation : NSOperation

 

@end

 

#import "PJXCustomOperation.h"

 

@implementation PJXCustomOperation

//在实现文件中实现-main方法

-(void)main

{

    NSLog(@"--1--%@",[NSThread currentThread]);

}

//自定义

-(void)customOperation

{

    PJXCustomOperation *cp = [[PJXCustomOperation alloc]init];

}

}

-(void)operation

{

    NSLog(@"---1---%@",[NSThread currentThread]);

}

NSOperation使用的三种方法