iOS 使用信号量处理多网络请求
2018-08-16 本文已影响0人
我一不小心就
@interface LoginViewController ()
{
dispatch_semaphore_t semaphore;
}
#pragma mark - #######actions######
- (void)queryAllAuthenInfo
{
//创建信号量
semaphore = dispatch_semaphore_create(0);
//创建全局并行队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
NSLog(@"处理网络请求A");
[self getPersonAuthenInfo];
});
dispatch_group_async(group, queue, ^{
NSLog(@"处理网络请求B");
[self getContactAuthenInfo];
});
dispatch_group_async(group, queue, ^{
NSLog(@"处理网络请求C");
[self getBankAuthenRes];
});
dispatch_group_async(group, queue, ^{
NSLog(@"处理网络请求D");
[self getCompanyAuthenRes];
});
dispatch_group_async(group, queue, ^{
NSLog(@"处理网络请求E");
[self getXinYuAuthenRes];
});
dispatch_group_notify(group, queue, ^{
//三个请求对应三次信号等待(很重要)
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"处理完网络请求A、B、C后,最后处理事件D:%@",[NSThread currentThread]);
dispatch_async(dispatch_get_main_queue(), ^{
[self loadMainTabbar];
});
});
}
#pragma mark - ###### query actions <##>######
- (void)getXinYuAuthenRes
{
[self.authenService getReputationDetailWithCallBack:^(ReputationDetail *repInfo) {
if (!isEmptyString(repInfo.zfbAccount)
|| !isEmptyString(repInfo.tbAccount))
{
[[NSUserDefaults standardUserDefaults] setBool:YES
forKey:kXinYuAuthenRes];
} else {
[[NSUserDefaults standardUserDefaults] setBool:NO
forKey:kXinYuAuthenRes];
}
dispatch_semaphore_signal(semaphore);
}];
}
- (void)getCompanyAuthenRes
{
[self.authenService getCompanyInfoWithCallBack:^(CompanyInfo *info) {
if (!isEmptyString(info.companyName))
{
[[NSUserDefaults standardUserDefaults] setBool:YES
forKey:kCompanyAuthenRes];
} else {
[[NSUserDefaults standardUserDefaults] setBool:NO
forKey:kCompanyAuthenRes];
}
dispatch_semaphore_signal(semaphore);
}];
}
- (void)getBankAuthenRes
{
[self.authenService getBankInfoWithCallBack:^(BankInfo *info) {
if (!isEmptyString(info.bank))
{
[[NSUserDefaults standardUserDefaults] setBool:YES
forKey:kBankInfoAuthenRes];
} else {
[[NSUserDefaults standardUserDefaults] setBool:NO
forKey:kBankInfoAuthenRes];
}
dispatch_semaphore_signal(semaphore);
}];
}
- (void)getPersonAuthenInfo
{
[self.authenService getUserInfoWithCallBack:^(PersonInfo *person) {
[[NSUserDefaults standardUserDefaults] setValue:person.name
forKey:kNameStr];
if (!isEmptyString(person.name))
{
[[NSUserDefaults standardUserDefaults] setBool:YES
forKey:kPersonInfoAuthenRes];
} else {
[[NSUserDefaults standardUserDefaults] setBool:NO
forKey:kPersonInfoAuthenRes];
}
dispatch_semaphore_signal(semaphore);
}];
}
- (void)getContactAuthenInfo
{
[self.authenService getContactsWithCallBack:^(ContactInfo *info) {
if (!isEmptyString(info.contactName1)
&& !isEmptyString(info.contactName2))
{
[[NSUserDefaults standardUserDefaults] setBool:YES
forKey:kContactAuthenRes];
} else {
[[NSUserDefaults standardUserDefaults] setBool:NO
forKey:kContactAuthenRes];
}
dispatch_semaphore_signal(semaphore);
}];
}
- (void)loadMainTabbar
{
RootTabViewController *rootVC = [[RootTabViewController alloc] init];
rootVC.tabBar.translucent = YES;
[self presentViewController:rootVC animated:YES completion:nil];
}