转自:http://blog.sina.com.cn/s/blog_7b9d64af0101c9sb.html
@interface CountingOperation : NSOperation{
}
- (id) initWithStartingCount:(NSUInteger)paramStartingCount endingCount:(NSUInteger)paramEndingCount;
@end
@implementation CountingOperation
NSUInteger startingCount;
NSUInteger endingCount;
BOOL finished;
BOOL executing;
- (id) init {
}
// 我们为我们自定义的operation,提供的初始化方法。这个初始化方法,根据我们的需要进行自定义
- (id) initWithStartingCount:(NSUInteger)paramStartingCount endingCount:(NSUInteger)paramEndingCount{
}
- (void) start {
}
- (void) main {
}
// 返回该operation是否完成
- (BOOL) isFinished{
}
// 返回该operation是否在执行
- (BOOL) isExecuting{
}
// 1.使用自己定义的同步operation,需要继承自NSOperation,并实现必要的方法:isFinished,isExecuting,main等,并实现KVO机制
// 2.如果不想让你自定义的operation与其他operation进行异步操作,你可以手动开始(调用start方法),并且在operation的start方法里面简单的调用main方法。
// 3.如果要自定义operation,需要继承资NSOperation。并且重载 isExecuting方法和isFinished方法。在这两个方法中,必须返回一个线程安全值(通常是个BOOL值),这个值可以在 operation 中进行操作。
// 4.一旦你的 operation 开始了,必须通过 KVO,告诉所有的监听者,现在该operation的执行状态。
// 5.在 operation 的 main 方法里面,必须提供 autorelease pool,因为你的 operation 完成后需要销毁。
// 6.必须为我们自定义的 operation 提供一个初始化方法。如:initWithStartingCount 方法。
@end
- (void)viewDidLoad{
//
}