记得做异步操作多线程操作时,有关于继承
NSOperation自定义同步Operation的操作。
当然也应该有继承
NSOperation
自定义同步Operation的操作。
今天研究了一下。mark!!!
关于继承NSOperation-自定义
同步
Operation 的操作,可以先看看我的另一篇博文:
今天研究了一下。mark!!
1.自定义自己的
NSOperation
操作类
该类跟同步方法的自定义类是一模一样的道理。
SimpleOperation.h 文件
@interface SimpleOperation : NSOperation{
NSString *givenObject;
BOOL finished;
BOOL executing;
}
// 定义初始化方法
- (id) initWithObject:(NSObject *)paramObject;
@end
SimpleOperation.m 文件
#import "SimpleOperation.h"
@implementation SimpleOperation
- (id) init {
NSNumber *dummyObject = [NSNumber numberWithInteger:123];
return([self initWithObject:dummyObject]);
}
// 定义初始化方法
- (id) initWithObject:(NSString *)paramObject{
self = [super init];
if (self != nil){
givenObject = paramObject;
NSLog(@"%@",paramObject);
}
return(self);
}
- (void) start {
if ([self isCancelled]){
[self willChangeValueForKey:@"isFinished"];
finished = YES;
[self didChangeValueForKey:@"isFinished"];
return;
} else {
[self willChangeValueForKey:@"isExecuting"];
executing = YES;
[self main];
[self didChangeValueForKey:@"isExecuting"];
}
}
- (void) main {
@try {
@autoreleasepool {
BOOL taskIsFinished = NO;
while (taskIsFinished == NO &&[self isCancelled] == NO){
NSLog(@"%s", __FUNCTION__);
NSLog(@"Parameter Object = %@", givenObject);
NSLog(@"Main Thread = %@", [NSThread mainThread]);
NSLog(@"Current Thread = %@", [NSThread currentThread]);
taskIsFinished = YES;
}
[self willChangeValueForKey:@"isFinished"];
[self willChangeValueForKey:@"isExecuting"];
finished = YES;
executing = NO;
[self didChangeValueForKey:@"isFinished"];
[self didChangeValueForKey:@"isExecuting"]; }
}
@catch (NSException * e) {
NSLog(@"Exception %@", e);
}
}
// 跟同步不同的地方。
// 必须重载Operation的实例方法 isConcurrent并在该方法中返回 YES
- (BOOL) isConcurrent{
return YES;
}
- (BOOL) isFinished{
return finished;
}
- (BOOL) isExecuting{
return executing;
}
@end
2.使用
NSOperationQueue
执行异步Operation
#import
@class AppDelegate;
@class SimpleOperation;
@class CountingOperation;
@interface MoreViewController : UIViewController{
}
@property (nonatomic, retain) NSOperationQueue *operationQueue;
@property (nonatomic, retain) SimpleOperation *firstOperation;
@property (nonatomic, retain) SimpleOperation *secondOperation;
@property (nonatomic, retain) SimpleOperation *threeOperation;
@property (nonatomic, retain) CountingOperation *countOperation;
@end
#import "SimpleOperation.h"
#import "CountingOperation.h"
@implementation MoreViewController
@synthesize operationQueue;
@synthesize firstOperation;
@synthesize secondOperation;
@synthesize threeOperation;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc{
[super dealloc];
}
- (void)viewDidLoad{
[super viewDidLoad];
CGRect btnControlRect=CGRectMake(200, 50, 100, 30);
UIButton *btnControl=[[UIButton alloc] initWithFrame:btnControlRect];
[btnControl setBackgroundColor:[UIColor redColor]];
[btnControl addTarget:self action:@selector(ClickControlAction:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnControl];
[btnControl release];
}
- (void)ClickControlAction:(id)sender{
NSLog(@"ClickControlAction");
}
- (void)viewDidAppear:(BOOL)animated{
NSString *firstString=[[NSString alloc] initWithFormat:@"firstString"];
NSString *secondString=[[NSString alloc] initWithFormat:@"secondString"];
NSString *threeString=[[NSString alloc] initWithFormat:@"threeString"];
// 初始化自定义的NSOperation对象
self.firstOperation = [[SimpleOperation alloc] initWithObject:firstString];
self.secondOperation = [[SimpleOperation alloc] initWithObject:secondString];
self.threeOperation = [[SimpleOperation alloc] initWithObject:threeString];
self.countOperation=[[CountingOperation alloc] initWithStartingCount:0endingCount:10000];
self.operationQueue = [[NSOperationQueue alloc] init];
// 将自定义的Operation添加到 NSOperationQueue
[self.operationQueue addOperation:self.firstOperation];
[self.operationQueue addOperation:self.secondOperation];
[self.operationQueue addOperation:self.threeOperation];
[self.operationQueue addOperation:self.countOperation];
NSLog(@"Main thread is here");
}
- (void)viewDidDisappear:(BOOL)animated{
[self.firstOperation release];
[self.secondOperation release];
[self.operationQueue release];
}
- (void)viewDidUnload{
[super viewDidUnload];
}
@end
输出:
firstString
secondString
threeString
Main thread is here
-[SimpleOperation main]
-[SimpleOperation main]
-[SimpleOperation main]
Parameter Object = firstString
Parameter Object = secondString
Parameter Object = threeString
Main Thread = {name = (null), num = 1}
Main Thread = {name = (null), num = 1}
Main Thread = {name = (null), num = 1}
Current Thread = {name = (null), num = 9}
Current Thread = {name = (null), num = 7}
Main Thread = {name = (null), num = 1}
Current Thread = {name = (null), num = 8}
Current Thread = {name = (null), num = 9}
DemoVideo[29240:290b] Count = 0
……
看到飘红的地方,是在不同的线程中执行的!
这样,就不会阻断主线程!
太多的理论已经说的很多,就不在赘述了!
自定义异步Operation依赖
// 同理,自定义的SimpleOperation也是可以进行依赖设置
[self.firstOperation addDependency:self.secondOperation];
[self.secondOperation addDependency:self.threeOperation];
[self.threeOperation addDependency:self.countOperation];
一句话:强大,很强大!!!
希望对你有所帮助!