美文网首页人猿星球
多网络请求--GCD

多网络请求--GCD

作者: qui丶MyLove | 来源:发表于2017-04-24 21:44 被阅读20次

dispatch_queue_t conCurrentGlobalQueue;//全局队列

dispatch_queue_t mainQueue ;//主队了

dispatch_group_t groupQueue;//队列组

此图循环位置放错了看下面代码

使用背景:当需求存在需要多个网络请求 都完成 之后 才能获取数据,界面刷行 ,同时请求存在需要顺序执行。故此,使用全局串型队列 ,开辟子线程异步执行网络请求,并将所有请求 依次放入group中,再所有请求完成后 刷新UI;

- (void)uploadFileWithImages:(NSArray*)images andSaveKey:(NSString*)saveKey andSuccess:(void(^)(NSArray* results))success andFail:(void(^)(NSArray*errors))fail{

conCurrentGlobalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

mainQueue = dispatch_get_main_queue();

groupQueue = dispatch_group_create();

__block NSMutableArray *result = [NSMutableArray array];

__block NSMutableArray *fails = [NSMutableArray array];

//设置空间名

[UPYUNConfig sharedInstance].DEFAULT_BUCKET = @"shyxserver";//shyxios p@ssw0rd

//设置空间表单密钥

[UPYUNConfig sharedInstance].DEFAULT_PASSCODE = @"GKRCIM2LmQGhfwredFang2hLhgk=";

//    for (UIImage *image in images) {

dispatch_async(conCurrentGlobalQueue,^{//全局队列  异步执行

dispatch_group_async(groupQueue, conCurrentGlobalQueue, ^{//将队列加入组中 异步执行

for (UIImage *image in images) {

__block UpYun *uy = [[UpYun alloc] init];//

//                        uy.successBlocker = success;

//                        uy.failBlocker = fail;

uy.successBlocker = ^(NSURLResponse *response, id responseData) {

//                                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"上传成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];

//                                [alert show];

NSLog(@"response body %@", responseData);

//                    if (success) {

//                        success(response,responseData);

//                    }

CCYunPaiModel *model = [CCYunPaiModel mj_objectWithKeyValues:responseData];

[result addObject:model];

dispatch_group_leave(groupQueue);//线程异步执行完 网络请求 离开组

};

uy.failBlocker = ^(NSError * error) {

//                                NSString *message = error.description;

//                                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"message" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];

//                                [alert show];

NSLog(@"error %@", error.description);

//                    if (fail) {

//                        fail(error);

//                    }

[fails addObject:error.description];

dispatch_group_leave(groupQueue);//线程异步执行完 网络请求 离开组

};

//                uy.progressBlocker = ^(CGFloat percent, int64_t requestDidSendBytes) {

//

//                };

//

//                        设置上传方式:分块上传 or 表单上传  默认表单上传,大文件需要分块上传

//                        uy.uploadMethod = UPMutUPload; //分块上传方式 or 表单上传方式

//

//

//                        如果 policy 由业务服务端生成, 这里只需要 return policy(提前从业务服务器获取的 policy),否则就不用初始化 policyBlocker

//                        uy.policyBlocker = ^()

//                        {

//                            return @"policy_created_by_app_server";

//                        };

//

//

//                        如果 sinature 由业务服务端签名, 这里只需要 return signature(提前从业务服务器获取的签名成功的 signature), 否则就不用初始化 signatureBlocker

//                        uy.signatureBlocker = ^(NSString *policy)

//                        {

//                            return @"GKRCIM2LmQGhfwredFang2hLhgk=";

//                        };

//

//

//                        根据 文件路径 上传

//                    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];

//                    NSString *filePath = [resourcePath stringByAppendingPathComponent:@"image.jpg"];

//                    [uy uploadFile:filePath saveKey:@"/test2.png"];

//

//

//                        直接上传 UIImage 类型数据

//                        UIImage * image = [UIImage imageNamed:@"test2.png"];

//                        [uy uploadFile:image saveKey:[self getSaveKeyWith:@"jpg"]];

//

//

//

//                        直接上传 NSData 类型数据

//                        NSData * fileData = [NSData dataWithContentsOfFile:filePath];

NSData *imageData = UIImagePNGRepresentation(image);

//                        [uy uploadImageData:fileData savekey:[self getSaveKeyWith:nil and:nil andTime:nil]];

//

//

//

//

//                        参数更详细的接口:可以设置 form 表单的 filename 及

//                        UIImage *image = [UIImage imageNamed:@"Default"];

//                        [uy uploadFile:image

//                              saveKey:[NSString stringWithFormat:@"/{year}/{mon}/c0974f7a-627a-44d6-9a70-dc977beb3447{.suffix}"]

//                              fileName:@"aa.png"

//                            extParams:nil];

dispatch_group_enter(groupQueue);//进入组中

dispatch_async(conCurrentGlobalQueue, ^{

[uy uploadImageData:imageData savekey:saveKey];

});

}

});

dispatch_group_notify(groupQueue, mainQueue, ^{//所有队列中的请求执行完成,组通知主队了刷行 ui

if (success) {

success(result);

}

if (fail) {

fail(fails);

}

NSLog(@"groupQueue中的任务 都执行完成,回到主线程更新UI");

});

});

//    }

}

相关文章

网友评论

    本文标题:多网络请求--GCD

    本文链接:https://www.haomeiwen.com/subject/tqqmzttx.html