• 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);
                }
            }
        }
    }
  • 相关阅读:
    记素质拓展
    操场边的人
    ASP.NET MVC FileResult介绍
    SQL SERVER 数据类型详解
    ASP.NET 使用Response.WriteFile方法下载文件
    Java 静态代理和动态代理
    单例模式
    设计模式六原则
    Python 单例模式
    JAVA WEB 中涉及的编解码
  • 原文地址:https://www.cnblogs.com/dlwj/p/6565917.html
Copyright © 2020-2023  润新知