我有一个嵌套的for循环。我想使用GCD并发来优化它。尝试:
我希望最终输出相同而不改变顺序。
-(NSMutableArray*)getsportNotificationObjectsByGroup{
NSMutableArray* notificationObjects = [NSMutableArray array];
NSString* defaultNoteValue;
defaultNoteValue = [WADeviceAndAppSettingPopUpManager getDefaultNoteValueBasedOnSystemAlertButtonAction];
for(WACategoryInfo * categoryInfo in self.allSportSCategories){
@autoreleasepool {
NSMutableArray *subArray = [[NSMutableArray alloc] init];;
for (NSDictionary *dictNotificationTags in categoryInfo.notificationTags) {
NSString *tagName, *tagCode;
BOOL isTagDefaultEnabled = NO;
tagName = [dictNotificationTags objectForKeyWithNullCheck:ktagName];
tagCode = [dictNotificationTags objectForKeyWithNullCheck:ktagCode];
isTagDefaultEnabled = [[dictNotificationTags objectForKeyWithNullCheck:kisTagDefaultEnabled] boolValue];
//********value computation*******//
NSString* val = @"";
if ([WAConfigLoader sharedInstance].isCollegeStyleApp) {
val = [WADeviceAndAppSettingPopUpManager getValueForSwitch:dictNotificationTags];
}
//*****value computation*********//
NSDictionary *sportDetail = @{
kname:tagName,
ktype:kswitch,
kid:tagCode,
kvalue : val,
ksportStringId:categoryInfo.sportStringID,
kTitleKey : tagName
};
[subArray addObject:sportDetail];
}
NSMutableDictionary *aNewDict = [[NSMutableDictionary alloc] init];
[aNewDict setObject:subArray forKey:kdata];
[aNewDict setObject:categoryInfo.sportTitle forKey:ksection];
[notificationObjects addObject:aNewDict];
}
}
return notificationObjects;
}
仅使用GCD不是强制性的,我关心的是最大限度地提高循环性能。
答案 0 :(得分:0)
如果用dispatch_apply
替换内循环,则必须关注这些缺点:
[subArray addObject]
subArray
中的订单未确定因此,我建议只将外部循环放在dispatch_apply
工作项中,因为它的结果会添加到不提供任何顺序的字典中。不过,您无论如何都必须同步[notificationObjects addObject]
。
请记住将dispatch_apply发送到并发队列。