NSOperation

NSOperation 抽象的基类

1、子类:

NSInvocationOperation & NSBlockOperation

 

NSOperationQueue 队列 在多线程中相当于线程池

 

addOperation

setMaxConcurrentOperation 

状态

ready cancelled(异步的操作cancelled之后还会执行完然后再进行状态判断)executing finished asynchronous

依赖(b要在a执行完之后才能执行的需求)

addDependency

 

 

 

 

 

 

 

 

-------------------------------

同步:

NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{

  for(int i=0; i<3;i++){

    [NSThread sleepForTimeInterval:1];

  }

}];

[blockOperation start];

异步:

NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{

  for(int i=0; i<3;i++){

    [NSThread sleepForTimeInterval:1];

  }

}];

if(self.operationQueue){

  self.operationQueue = [[NSOperationQueue alloc] init];

}

  [self.operationQueue addOperationQueue:blockOperatin];//(实现一异步)

 

[A addDependency:B];

[B addDependency:C];

[C addDependency:D];

切记不要形成闭环 否则会死锁

 

+ (void)uploadImages:(NSArray<UIImage *> *)images isAsync:(BOOL)isAsync complete:(void(^)(NSArray<NSString *> *names, UploadImageState state))complete
{
    id<OSSCredentialProvider> credential = [[OSSPlainTextAKSKPairCredentialProvider alloc] initWithPlainTextAccessKey:AccessKeysecretKey:SecretKey];

    OSSClient *client = [[OSSClient alloc] initWithEndpoint:AliYunHost credentialProvider:credential];

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    queue.maxConcurrentOperationCount = images.count;

    NSMutableArray *callBackNames = [NSMutableArray array];
    int i = 0;
    for (UIImage *image in images) {
        if (image) {
            NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
                //任务执行
                OSSPutObjectRequest * put = [OSSPutObjectRequest new];
                put.bucketName = BucketName;
                NSString *imageName = [kTempFolder stringByAppendingPathComponent:[[NSUUID UUID].UUIDString stringByAppendingString:@".jpg"]];
                put.objectKey = imageName;
                [callBackNames addObject:imageName];
                NSData *data = UIImageJPEGRepresentation(image, 0.3);
                put.uploadingData = data;

                OSSTask * putTask = [client putObject:put];
                [putTask waitUntilFinished]; // 阻塞直到上传完成
                if (!putTask.error) {
                    NSLog(@"upload object success!");
                } else {
                    NSLog(@"upload object failed, error: %@" , putTask.error);
                }
                if (isAsync) {
                    if (image == images.lastObject) {
                        NSLog(@"upload object finished!");
                        if (complete) {
                            complete([NSArray arrayWithArray:callBackNames] ,UploadImageSuccess);
                        }
                    }
                }
            }];
            if (queue.operations.count != 0) {
                [operation addDependency:queue.operations.lastObject];
            }
            [queue addOperation:operation];
        }
        i++;
    }
    if (!isAsync) {
        [queue waitUntilAllOperationsAreFinished];
        NSLog(@"haha");
        if (complete) {
            if (complete) {
                complete([NSArray arrayWithArray:callBackNames], UploadImageSuccess);
            }
        }
    }
}
posted @ 2017-03-17 14:24  iWillW  阅读(176)  评论(0编辑  收藏  举报