迭代for循环时变慢

时间:2015-11-24 07:07:13

标签: ios iphone

for(NSlictionary * detailsDict in rsltArray)     {             NSString * OfficeId = [detailsDict objectForKey:@“ObjectId”];             NSString * catID = [detailsDict objectForKey:@“catID”];             NSString * ObjType = [detailsDict objectForKey:@“ObjectType”];

        NSString *getImage = [NSString stringWithFormat:@"select catMarkericon from TBL_Category where catID='%@'",catID];
        NSMutableArray * imageArray = [self.database getDataListBySQLQueryStatement:getImage];
        NSString *officeLogo  = [[imageArray objectAtIndex:0] objectForKey:@"catMarkericon"];

        NSArray *parts = [officeLogo componentsSeparatedByString:@"/"];
        NSString *filename = [parts lastObject];


        NSString *OfficeName;
        NSString *OfficeAddress;
        if([[UserDefaults getLanguageSelection]isEqualToString:@"en"])
        {
            OfficeName  =[detailsDict objectForKey:@"ObjectNameEnglish"];
            OfficeAddress  =[detailsDict objectForKey:@"ObjectAddressEnglish"];
        }
        else
        {
            OfficeName  =[detailsDict objectForKey:@"ObjectNameArabic"];
            OfficeAddress  =[detailsDict objectForKey:@"ObjectAddressArabic"];
        }

        double Off_lat      =[[detailsDict objectForKey:@"Latitude"] doubleValue];
        double Off_long     =[[detailsDict objectForKey:@"Longitude"] doubleValue];

        tempLocation = [[CLLocation alloc] initWithLatitude:Off_lat longitude:Off_long];
        CLLocationDistance meters = [tempLocation distanceFromLocation:CurrentLoc]/1000;

        if(meters<=Dist)
        {
            [arrListdata addObject:detailsDict];
            if ([catID isEqualToString:selectID]) {
                tempCoordinate = [ARGeoMarkerco coordinateWithLocation:tempLocation locationTitle:OfficeName withid:OfficeId withaddress:OfficeAddress withimage:filename withcatId:catID withObjType:ObjType selectValue:YES newmarker:NO];
                [locationArray addObject:tempCoordinate];
            }
            else{
                tempCoordinate = [ARGeoMarkerco coordinateWithLocation:tempLocation locationTitle:OfficeName withid:OfficeId withaddress:OfficeAddress withimage:filename withcatId:catID withObjType:ObjType selectValue:NO newmarker:NO];
                [locationArray addObject:tempCoordinate];
            }

    }

}

我正在使用这样的循环,我必须迭代超过1500个数据,它会让我的应用程序卡住4到7秒,无论如何解决这个问题

1 个答案:

答案 0 :(得分:-1)

我必须用Objective C / pseudocode hybrid写这个,只是为了尽可能快地给你一个答案。

将1500个成员数组拆分为2个。

NSRange theRange;
NSRange theSecondRange;
theRange.location = 0;
theRange.length = 749;
theSecondRange.location = 750;
theSecondRange.length = 750;    
NSArray *array1 = [rsltArray subarrayWithRange:theRange];
NSArray *array2 = [rsltArray subarrayWithRange:theSecondRange];

然后,在后台创建2个调度队列。使用您拥有的代码将第一个数组在第一个队列中线程化。对第二个做同样的事情。线程完成后,使用NSArray arrayByAddingObjectsFromArray将它们放在一起。

如果它仍然太慢,请将数组进一步拆分为4或8,然后将它们分成不同的队列。诀窍是你必须等待添加它们,直到线程返回,否则你的应用程序将崩溃,因为没有一个数组可以添加回一个主阵列。 更新在Apple上关于Grand Central Dispatch中的调度组的链接,以处理我提到的关于子阵列何时返回的情况https://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/index.html#//apple_ref/doc/uid/TP40008079-CH2-SW19

相关问题