iOS 之runloop理解
起初有个业务需求需要你获取完服务器数据在拿着服务器数据做处理 有人有可能会想到用gcd啊像信号量什么巴拉巴吧的可以实现啊但是少年可以是可以但如果你这边服务器数据请求完之后需要你有返回值了 return什么的 那么这个就实现不了咯 ,经过思索后最后用runloop实现了
先贴代码
/ add by lican 7.6.0 增加投顾业务试用权限功能 2020 5/1
pragma mark - 判断当前用户是否有投顾业务试用权限并且只有该接口请求完才会走下一步的逻辑
-
(NSMutableDictionary)requestTGBusinessInfoWithUserCode:(NSString)userCode DicPara:(NSDictionary*)dicPara
{
NSMutableDictionary *pDict = NewObjectAutoD(NSMutableDictionary);
[pDict setTztObject:@"check" forKey:@"funcType"];
[pDict setTztObject:userCode forKey:@"userCode"];CFRunLoopRef ref = CFRunLoopGetCurrent();//获取线程runloop
CFRunLoopSourceContext ctx = {0};
//给他个source 监听 如果不加的话在开个多线程就不会阻塞了跳出了你的请求不等你请求返回结果了直接去搞UI啥子事去了
CFRunLoopSourceRef source = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &ctx);
CFRunLoopAddSource(ref, source, kCFRunLoopDefaultMode);
[[self requestHandle] requestWithSession:ZTRequestSessionZX AndSendValue:pDict action:@"48650" successNo:0 success:^(NSDictionary *dicResult, int nAction, NSDictionary *dicSendValue) {
NSDictionary *dictData = (NSDictionary *)dicResult;
//主题信息
NSString *strData = [dictData tztObjectForKey:@"DATA"];
//容错处理,@""解析后为nil
if (strData == nil)
strData = @"";
NSArray *arrJsonData = [NSJSONSerialization JSONObjectWithData:[strData dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
NSMutableArray *arrtgBusinessInfo = NewObjectAutoD(NSMutableArray);
//只有状态是待领取的才能插入到本地数组
for (NSDictionary *tgBusinessInfoDic in arrJsonData)
{
if([[tgBusinessInfoDic objectForKey:@"state"] isEqualToString:@"pending"])
{
[arrtgBusinessInfo addObject:tgBusinessInfoDic];
}} [[tztUserSaveDataObj getShareInstance]setUserDataObject:arrtgBusinessInfo ForKey:tztTradeLoginBindAccountTGBusinessInfo]; //收到请求跑圈结束吧不用阻塞啦 CFRunLoopRemoveSource(ref, source, kCFRunLoopDefaultMode); CFRunLoopStop(ref);//取消阻塞 CFRelease(source);
} failure:^(int nErrNo, NSString *strErrMsg, int nAction, NSDictionary *dicSendValue, id dicResult) {
//收到错误的请求跑圈也结束吧不用阻塞啦
CFRunLoopRemoveSource(ref, source, kCFRunLoopDefaultMode);
CFRunLoopStop(ref);//取消阻塞
CFRelease(source);
}];
//好我们开始跑圈吧 只有你 停了我才走下面的for循环便利等一系列逻辑咯
CFRunLoopRun();
NSMutableDictionary *dicTGBusinessInfo = NewObjectAutoD(NSMutableDictionary);
NSArray *arrTGBusinessInfo = (NSArray *)[[tztUserSaveDataObj getShareInstance]getUserDataObjectForKey:tztTradeLoginBindAccountTGBusinessInfo];
NSString *strBuyType = [dicPara objectForKey:@"type"];
// 遍历所有的投顾试用权限业务当类型一致并且产品ID一致就说明该业务可以试用
for (NSDictionary *tgBusinessInfoDic in arrTGBusinessInfo)
{
if (strBuyType.integerValue ==1&&[[tgBusinessInfoDic objectForKey:@"productType"] isEqualToString:@"TG"]&&[[tgBusinessInfoDic objectForKey:@"productId"] isEqualToString:dicPara[@"id"]])
{
[dicTGBusinessInfo setTztValue:@"1" forKey:@"onlyCode"];
[dicTGBusinessInfo setTztValue:[tgBusinessInfoDic objectForKey:@"productCode"] forKey:@"productCode"];
}
if (strBuyType.integerValue ==2&&[[tgBusinessInfoDic objectForKey:@"productType"] isEqualToString:@"JN"]&&[[tgBusinessInfoDic objectForKey:@"productId"] isEqualToString:dicPara[@"id"]])
{
[dicTGBusinessInfo setTztValue:@"1" forKey:@"onlyCode"];
[dicTGBusinessInfo setTztValue:[tgBusinessInfoDic objectForKey:@"productCode"] forKey:@"productCode"];
}
if (strBuyType.length<=0 &&[[tgBusinessInfoDic objectForKey:@"productType"] isEqualToString:@"ZH"]&&[[tgBusinessInfoDic objectForKey:@"productId"] isEqualToString:dicPara[@"id"]])
{
[dicTGBusinessInfo setTztValue:@"1" forKey:@"onlyCode"];
[dicTGBusinessInfo setTztValue:[tgBusinessInfoDic objectForKey:@"productCode"] forKey:@"productCode"];
}
}
return dicTGBusinessInfo;
}