[TOC]
NSOperation
基于GCD的封装!
API介绍
//队列,看做线程
@interface NSOperationQueue : NSObject
//操作,一个操作对应一个方法
@interface NSOperation : NSObject
//NSOperation 是一个"抽象类",不能直接使用; 抽象类的用处是定义子类共有的属性和方法
@interface NSBlockOperation : NSOperation //(block)
@interface NSInvocationOperation : NSOperation //(target-action)
对列:queue
@interface NSOperationQueue : NSObject
//The NSOperationQueue class regulates the execution of a set of NSOperation objects.
//Managing Operations in the Queue
- (void)addOperation:(NSOperation *)op;
- (void)addOperations:(NSArray<NSOperation *> *)ops waitUntilFinished:(BOOL)wait ;
- (void)addOperationWithBlock:(void (^)(void))block;
@property (readonly, copy) NSArray<__kindof NSOperation *> *operations;
@property (readonly) NSUInteger operationCount;
@property (getter=isSuspended) BOOL suspended;
- (void)cancelAllOperations;
- (void)waitUntilAllOperationsAreFinished; //Blocks the current thread until all of the receiver’s queued and executing operations finish executing.
//When called, this method blocks the current thread and waits for the receiver’s current and queued operations to finish executing.
//创建队列
@property (class, readonly, strong, nullable) NSOperationQueue *currentQueue;
@property (class, readonly, strong) NSOperationQueue *mainQueue;
//属性设置
@property (nullable, copy) NSString *name;
@property NSQualityOfService qualityOfService;
@property (nullable, assign /* actually retain */) dispatch_queue_t underlyingQueue;
@property NSInteger maxConcurrentOperationCount; //线程最大并发数量
@end
操作:
@interface NSOperation : NSObject
typedef NS_ENUM(NSInteger, NSOperationQueuePriority) {
NSOperationQueuePriorityVeryLow = -8L,
NSOperationQueuePriorityLow = -4L,
NSOperationQueuePriorityNormal = 0,
NSOperationQueuePriorityHigh = 4,
NSOperationQueuePriorityVeryHigh = 8
};
typedef enum NSQualityOfService : NSInteger {
NSQualityOfServiceUserInteractive = 0x21,
NSQualityOfServiceUserInitiated = 0x19,
NSQualityOfServiceUtility = 0x11,
NSQualityOfServiceBackground = 0x09,
NSQualityOfServiceDefault = -1
} NSQualityOfService;
//创建线程
- (id)init;
//线程设置
@property (nullable, copy) NSString *name;
@property (nullable, copy) void (^completionBlock)(void) ; //线程结束后の操作
@property NSOperationQueuePriority queuePriority;
@property NSQualityOfService qualityOfService;
//操作
- (void)start;
- (void)cancel;
//Status
@property (readonly, getter=isCancelled) BOOL cancelled;
@property (readonly, getter=isExecuting) BOOL executing;
@property (readonly, getter=isFinished) BOOL finished;
@property (readonly, getter=isConcurrent) BOOL concurrent; // To be deprecated; use and override 'asynchronous' below
@property (readonly, getter=isAsynchronous) BOOL asynchronous;
@property (readonly, getter=isReady) BOOL ready;
//Dependency从属
- (void)addDependency:(NSOperation *)op; //Makes the receiver dependent on the completion of the specified operation.
- (void)removeDependency:(NSOperation *)op;
@property (readonly, copy) NSArray<NSOperation *> *dependencies;
- (void)waitUntilFinished;
- (void)main; //You should override this method to perform the desired<渴望的> task. In your implementation, do not invoke super.
@end
//The NSBlockOperation class is a concrete subclass of NSOperation that manages the concurrent execution of one or more blocks.
@interface NSBlockOperation : NSOperation
+ (instancetype)blockOperationWithBlock:(void (^)(void))block;
- (void)addExecutionBlock:(void (^)(void))block;
@property (readonly, copy) NSArray<void (^)(void)> *executionBlocks;
@end
@interface NSInvocationOperation : NSOperation //The NSInvocationOperation class is a concrete subclass of NSOperation that manages the execution of a single encapsulated task specified as an invocation.
- (nullable instancetype)initWithTarget:(id)target selector:(SEL)sel object:(nullable id)arg;
- (instancetype)initWithInvocation:(NSInvocation *)inv ;
@property (readonly, retain) NSInvocation *invocation;
@property (nullable, readonly, retain) id result;
@end