标签:
NSOperation简介
1.NSOperation的作用
配合使用NSOperation和NSOperationQueue也能实现多线程编程
2.NSOperation和NSOperationQueue实现多线程的具体步骤
NSOperation的子类
NSInvocationOperation
使用NSInvocationOperation的步骤:
- (id)initWithTarget:(id)target selector:(SEL)sel object:(id)arg;
- (void)start;
3.在target中实现sel方法。一旦执行操作,就会调用target的sel方法
+ (id)blockOperationWithBlock:(void (^)(void))block;
- (void)addExecutionBlock:(void (^)(void))block;
注意:只要NSBlockOperation封装的操作数 > 1,就会异步执行操作
- (void)addOperation:(NSOperation *)op; - (void)addOperationWithBlock:(void (^)(void))block;
最大并发数
- (NSInteger)maxConcurrentOperationCount; - (void)setMaxConcurrentOperationCount:(NSInteger)cnt;
队列的取消、暂停、恢复
- (void)cancelAllOperations;
提示:也可以调用NSOperation的- (void)cancel方法取消单个操作
- (void)setSuspended:(BOOL)b; // YES代表暂停队列,NO代表恢复队列 - (BOOL)isSuspended;
操作优先级
- (NSOperationQueuePriority)queuePriority; - (void)setQueuePriority:(NSOperationQueuePriority)p;
NSOperationQueuePriorityVeryLow = -8L, NSOperationQueuePriorityLow = -4L, NSOperationQueuePriorityNormal = 0, NSOperationQueuePriorityHigh = 4, NSOperationQueuePriorityVeryHigh = 8
操作的监听
- (void (^)(void))completionBlock; - (void)setCompletionBlock:(void (^)(void))block;
操作依赖
[operationB addDependency:operationA]; // 操作B依赖于操作A
标签:
原文地址:http://www.cnblogs.com/JackieHoo/p/5040592.html