iOS 队列请求-超实用

用法解析:

比如我现在要到服务端请求十几个pdf文件,下载到本地Document目录,那么用这个就非常好,

好处一:不会卡死主线程

好处二:下载完成有回调,可以在下载完成后,做一些刷新UI的操作,爽歪歪。


实例操作:

-(void)downloadPdfFileWithUrl:(NSArray *)modelList{

    if(self.config.hasUpdateData)return;

    NSMutableArray *mutableOperations = [NSMutableArray array];

    int countIndex = 0;

    for (MMPGetManualElementManualList *Model in modelList)

    {

        NSString *fileName = Model.manualName;

        [[NSUserDefaults standardUserDefaults] setValue:fileName forKey:[NSString stringWithFormat:@"%@%d",fileNameKey,countIndex]];

        NSString *pdf_diret = Model.annexPath;

        

        NSString *fileContentPath = [self getManualPath];

        NSString *pdf_loc_path = pdf_diret.lastPathComponent;

        

        NSString *pdf_url_path = [NSString stringWithFormat:@"%@/%@",baseURL,pdf_diret];

        NSString *abusolutePath = [fileContentPath stringByAppendingPathComponent:pdf_loc_path];

        

        [pdf_path_array addObject:pdf_loc_path];

        

        countIndex++;

        

        BOOL existFile = [[NSFileManager defaultManager] fileExistsAtPath:abusolutePath isDirectory:NO];

        if (existFile && [self fileSizeAtPath:abusolutePath] == 0) {

            [self removeFileAtPath:abusolutePath];

        }else if(existFile){

            continue;

        }

        

        NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:[pdf_url_path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60.f];

        AFHTTPRequestOperation *operation =   [[AFHTTPRequestOperation alloc] initWithRequest:request];

        [operation setOutputStream: [NSOutputStream outputStreamToFileAtPath:abusolutePath append:NO]];

        [mutableOperations addObject:operation];

    }

    


    __block CGFloat progress = 0.0f;

    NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:mutableOperations progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {

        progress = (float)numberOfFinishedOperations/totalNumberOfOperations;

       NSLog(@"%f complete", progress);

    } completionBlock:^(NSArray *operations) {

        NSLog(@"All operations in batch complete");

        MMActivityIndicator_stop();

        self.service.customLoadingHint = NO;

        self.config.hasUpdateData = YES;

    }];

    


    

    [[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值