0

我这样做是在应用程序启动后立即执行的,幸运的是我只需要在一个名为 CMIDataManager 的单例类中执行一次,我的应用程序启动时间太长。

该 plist 包含:

指挥官.plist:

德语 - 数组

苏联 - 阵列

每个指挥官阵列有 19 个指挥官,每个指挥官有 5 种能力(通过独特的能力 uid 映射)。

能力.plist:

GermanAbilities - 数组

苏联能力 - 阵列

每个数组包含 40 个具有 uid 的能力(用于将指挥官映射到能力)

一开始,我需要创建一个模型类,因此我针对每个隐藏的能力迭代指挥官的能力 uid,一旦找到匹配项,我将能力模型对象添加到 Commaders 模型对象。

我怎样才能更快地做到这一点?使用基于块的枚举会加快速度吗?我该如何使用它?

-(void)loadCommandersAndAbilities{

#pragma German Abilities iteration

NSString* abilitiesPlistPath = [[NSBundle mainBundle] pathForResource:@"Abilities" ofType:@"plist"];

NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:abilitiesPlistPath];


NSArray *tempArray = [dictionary objectForKey:@"GermanAbilities"];

NSArray *tempArray2 = [dictionary objectForKey:@"SovietAbilities"];


NSMutableArray *tempAbilitiesArray = [[NSMutableArray alloc] initWithCapacity:tempArray.count];

for (NSDictionary *dict in tempArray) {
    Ability *ability = [[Ability alloc] init];

    [ability populateWithDictionary:dict];

    [tempAbilitiesArray addObject:ability];

    NSLog(@"Adding object %@ to temp abilities",ability.name);

}

self.germanAbilitiesArray = [NSArray arrayWithArray:tempAbilitiesArray];

[tempAbilitiesArray removeAllObjects];


#pragma Soviet abilities iteration

for (NSDictionary *dict in tempArray2) {
    Ability *ability = [[Ability alloc] init];

    [ability populateWithDictionary:dict];

    [tempAbilitiesArray addObject:ability];

}

self.sovietAbilitiesArray = [NSArray arrayWithArray:tempAbilitiesArray];



#pragma German commander itertation


NSString* commandersPlistPath = [[NSBundle mainBundle] pathForResource:@"Commanders" ofType:@"plist"];


dictionary = [[NSDictionary alloc] initWithContentsOfFile:commandersPlistPath];

tempArray = [dictionary objectForKey:@"German"];

tempArray2 = [dictionary objectForKey:@"Soviet"];

NSLog(@"Temp German commadner array is %@", tempArray);

NSLog(@"Temp Soviet commadner array is %@", tempArray2);


NSMutableArray *tempCommandersArray = [[NSMutableArray alloc] initWithCapacity:tempArray.count];

NSMutableArray *tempCommandersArray2 = [[NSMutableArray alloc] initWithCapacity:tempArray2.count];


for (NSDictionary *dict in tempArray) {
    Commander *commander = [[Commander alloc] init];

    [commander populateWithDictionary:dict];

    for (NSNumber *uid in commander.abilitiesUIDArray) {

        NSLog(@"uid %@", uid);
        for (Ability *ability in self.germanAbilitiesArray) {

            NSLog(@"ability uid is : %@, target uid %@ ",ability.uid, uid);
            if ([ability.uid isEqualToNumber: uid]) {


                NSLog(@"Adding abilty %@ to commander %@: ",ability.name, commander.name);


                [commander.abilitiesArray addObject:ability];


                NSLog(@"Current commander abilty array is  %@: ",commander.abilitiesArray);

            }


        }
    }

    [tempCommandersArray addObject:commander];

}


self.germanCommandersArray = [NSArray arrayWithArray:tempCommandersArray];

NSLog(@"Final german Commaders %@",self.germanCommandersArray);



#pragma Soviet commander itertation


for (NSDictionary *dict in tempArray2) {
    Commander *commander = [[Commander alloc] init];

    [commander populateWithDictionary:dict];

    for (NSNumber *uid in commander.abilitiesUIDArray) {

        NSLog(@"uid %@", uid);
        for (Ability *ability in self.sovietAbilitiesArray) {

            NSLog(@"ability uid is : %@, target uid %@ ",ability.uid, uid);
            if ([ability.uid isEqualToNumber: uid]) {


                NSLog(@"Adding abilty %@ to commander %@: ",ability.name, commander.name);


                [commander.abilitiesArray addObject:ability];


                NSLog(@"Current commander abilty array is  %@: ",commander.abilitiesArray);

            }


        }
    }

    [tempCommandersArray2 addObject:commander];

}


self.sovietCommandersArray = [NSArray arrayWithArray:tempCommandersArray2];

NSLog(@"Final Soviet Commaders %@",self.germanCommandersArray);




}

添加图像:

指挥官 Plist 样本 能力清单样本

4

2 回答 2

1

显而易见的是,您的能力数组不应该是数组,而是字典。这样您就不用将 uid 与每个能力的 uid 进行比较,而是在一次操作中查找它。

于 2014-03-05T21:28:29.890 回答
1

似乎问题出在这个循环中:

        if ([ability.uid isEqualToNumber: uid]) {

                [commander.abilitiesArray addObject:ability];

}

一旦我在所有能力列表中找到了指挥官能力的匹配项,我就不需要检查该能力是否与其他能力匹配,所以我添加了一个 break 语句。

            if ([ability.uid isEqualToNumber: uid]) {


                //NSLog(@"Adding abilty %@ to commander %@: ",ability.name, commander.name);


                [commander.abilitiesArray addObject:ability];


                //NSLog(@"Current commander abilty array is  %@: ",commander.abilitiesArray);
                break;
            }

我还将它添加到代码中以使其在后台线程上运行,从而将启动时间从 6 秒缩短到 0.5 秒。

-(instancetype)init {

self = [super init];

if(self) {

    [self performSelectorInBackground:@selector(loadCommandersAndAbilities) withObject:nil];

    //[self loadCommandersAndAbilities];

   // NSOperationQueue
}

return self;
}

loadCommandersAndAbilities: 是原始问题中列出的方法,我还添加了通知让我的视图控制器知道该方法何时完成。

// loadCommandersAndAbilities 结束

[[NSNotificationCenter defaultCenter] postNotificationName:@"TableViewDataDownloaded" object:nil];
于 2014-03-05T21:28:40.910 回答