@property(nonatomic,strong) NSInvocationOperation *simpleOperation;
@property(nonatomic,strong) NSBlockOperation *blockOpertaion;
@synthesize simpleOperation = _simpleOperation;
@synthesize blockOpertaion = _blockOpertaion;
- (void) simpleOperationEntry:(id) paramObject {
NSLog(@"Parameter Object = %@",paramObject);
NSLog(@"Main Thread = %@",[NSThread mainThread]);
NSLog(@"Current Thread = %@",[NSThread currentThread]);
}
调用代码
-(void) viewDidAppear:(BOOL)animated {
self.simpleOperation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(simpleOperationEntry:)
[self.simpleOperation start];
self.blockOpertaion = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"Main Thread = %@",[NSThread mainThread]);
NSLog(@"Current Thread = %@",[NSThread currentThread]);
NSUInteger count = 0;
for (count=0; count<10; ++count) {
NSLog(@"count= %d",count);
}
}];
}
2013-10-24 16:28:46.269 OperationDemo[5459:c07] Main Thread = <NSThread: 0x717ae10>{name = (null), num = 1}
2013-10-24 16:38:07.240 OperationDemo[5478:c07] count= 0
2013-10-24 16:38:07.241 OperationDemo[5478:c07] count= 1
2013-10-24 16:38:07.241 OperationDemo[5478:c07] count= 2
2013-10-24 16:38:07.241 OperationDemo[5478:c07] count= 3
2013-10-24 16:38:07.241 OperationDemo[5478:c07] count= 4
2013-10-24 16:38:07.242 OperationDemo[5478:c07] count= 5
2013-10-24 16:38:07.242 OperationDemo[5478:c07] count= 6
2013-10-24 16:38:07.242 OperationDemo[5478:c07] count= 7
2013-10-24 16:38:07.243 OperationDemo[5478:c07] count= 8
2013-10-24 16:38:07.243 OperationDemo[5478:c07] count= 9
@property (nonatomic, strong) NSOperationQueue *operationQueue;
@property (nonatomic, strong) NSInvocationOperation *firstOperation;
@property (nonatomic, strong) NSInvocationOperation *secondOperation;
@synthesize operationQueue = _operationQueue;
@synthesize firstOperation =_firstOperation;
@synthesize secondOperation =_secondOperation;
NSLog(@"%s", __FUNCTION__);
NSLog(@"Parameter Object = %@", paramObject);
NSLog(@"Main Thread = %@", [NSThread mainThread]);
NSLog(@"Current Thread = %@", [NSThread currentThread]);
}
- (void) secondOperationEntry:(id)paramObject{
NSLog(@"%s", __FUNCTION__);
NSLog(@"Parameter Object = %@", paramObject);
NSLog(@"Main Thread = %@", [NSThread mainThread]);
NSLog(@"Current Thread = %@", [NSThread currentThread]);
self.secondOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(secondOperationEntry:) object:[NSNumber numberWithInteger:222] ];
2013-10-25 16:41:57.103 OperationDemo[8424:1903] Parameter Object = 111
2013-10-25 16:41:57.103 OperationDemo[8424:1903] Main Thread = <NSThread: 0x71216f0>{name = (null), num = 1}
2013-10-25 16:41:57.105 OperationDemo[8424:1903] Current Thread = <NSThread: 0x7579120>{name = (null), num = 3}
2013-10-25 16:41:57.102 OperationDemo[8424:4607] -[WBViewController secondOperationEntry:]
2013-10-25 16:41:57.106 OperationDemo[8424:4607] Parameter Object = 222
2013-10-25 16:41:57.106 OperationDemo[8424:4607] Main Thread = <NSThread: 0x71216f0>{name = (null), num = 1}
2013-10-25 16:41:57.107 OperationDemo[8424:4607] Current Thread = <NSThread: 0x7578fd0>{name = (null), num = 4}
self.secondOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(secondOperationEntry:) object:[NSNumber numberWithInteger:222] ];
// [self.firstOperation removeDependency:self.secondOperation] //解除依赖关系
[self.firstOperation addDependency:self.secondOperation];
NSLog(@"Curent Thread ID:=%@",[NSThread currentThread]);
NSLog(@"######:Hello World");
b.重写 isStart方法 操作队列会调用该方法来执行操作
c.重写 isCancelled方法 操作队列会周期性调用该方法查看当前操作的执行情况
d.重写 main方法,并创建自己的自动回收池,在main函数返回前来释放资源回收池
e.重写 isFinished和isExecuting方法来反馈跟踪操作执行情况
@interface CountingOperation : NSOperation
@property(nonatomic,assign) BOOL isFinished;
@property(nonatomic,assign) BOOL isExecuting;
-(id) initWithStartingCount:(NSInteger) startCount
endingCount:(NSInteger) end;
@end
@interface CountingOperation()
@property(nonatomic,assign) NSInteger startCount;
@property(nonatomic,assign) NSInteger end;
@end
@implementation CountingOperation
@synthesize startCount = _startCount;
@synthesize end = _end;
@synthesize isFinished = _isFinished;
@synthesize isExecuting = _isExecuting;
-(id) init
{
return [self initWithStartingCount:0 endingCount:100];
}
-(id) initWithStartingCount:(NSInteger) startCount
endingCount:(NSInteger) end
{
if (self = [super init])
{
self.startCount = startCount;
self.end = end;
}
return self;
}
//Conflicting return type in implementation of 'start': 'NSInteger' (aka 'int') vs 'void'
- (void) start
{
if ([self isFinished])
{
[self willChangeValueForKey:@"isFinished"];
self.isFinished = YES;
[self didChangeValueForKey:@"isFinished"];
return;
}
else
{
/* If this operation is *not* cancelled */
/* KVO compliance */
[self willChangeValueForKey:@"isExecuting"];
self.isExecuting = YES;
/* Call the main method from inside the start method */
[self didChangeValueForKey:@"isExecuting"];
[self main];
}
}
- (void) main
{
@try {
/* Here is the autorelease pool */
@autoreleasepool
{
NSLog(@"######:Current Thread = %@", [NSThread currentThread]);
/* Keep a local variable here that must get set to YES whenever we are done with the task */
BOOL taskIsFinished = NO;
/* Create a while loop here that only exists
if the taskIsFinished variable is set to YES or the operation has been cancelled */
while (taskIsFinished == NO && [self isCancelled] == NO)
{
/* Perform the task here */
NSLog(@"Main Thread = %@", [NSThread mainThread]);
NSLog(@"Current Thread = %@", [NSThread currentThread]);
NSUInteger counter = self.startCount;
for (counter = self.startCount;counter < self.end;counter++)
{
NSLog(@"Count = %lu", (unsigned long)counter);
}
/* Very important. This way we can get out of the
loop and we are still complying with the cancellation rules of operations */
taskIsFinished = YES;
}
/* KVO compliance. Generate the required KVO notifications */
[self willChangeValueForKey:@"isFinished"];
[self willChangeValueForKey:@"isExecuting"];
self.isFinished = YES;
self.isExecuting = NO;
[self didChangeValueForKey:@"isFinished"];
[self didChangeValueForKey:@"isExecuting"];
}
}
@catch (NSException * e)
{
NSLog(@"Exception %@", e);
}
}
- (BOOL) isFinished{
/* Simply return the value */
return(_isFinished);
}
- (BOOL) isExecuting{
/* Simply return the value */
return(_isExecuting);
}
- (BOOL) isConcurrent{
return YES;
}
2013-10-25 17:06:05.607 OperationDemo[8562:4803] Current Thread = <NSThread: 0x717b350>{name = (null), num = 3}
2013-10-25 17:06:05.608 OperationDemo[8562:4803] Count = 0
2013-10-25 17:06:05.616 OperationDemo[8562:4803] Count = 1
2013-10-25 17:06:05.616 OperationDemo[8562:4803] Count = 2
2013-10-25 17:06:05.617 OperationDemo[8562:4803] Count = 3
2013-10-25 17:06:05.617 OperationDemo[8562:4803] Count = 4
2013-10-25 17:06:05.617 OperationDemo[8562:4803] Count = 5
2013-10-25 17:06:05.617 OperationDemo[8562:4803] Count = 6
2013-10-25 17:06:05.618 OperationDemo[8562:4803] Count = 7
2013-10-25 17:06:05.618 OperationDemo[8562:4803] Count = 8
2013-10-25 17:06:05.619 OperationDemo[8562:4803] Count = 9
922



被折叠的 条评论
为什么被折叠?



