Core data performance on ios 5

61 views Asked by At

I need to optimise for core data on ios 5 device. Although it is fast on ios 6 and above, it is very slow on ios 5 device. I saw log like this.

CoreData: annotation: total fetch execution time: 1.0510s for 11 rows.

May I know what is wrong with my codes? Is there other good way to optimise core data performance on ios 5 device?

- (NSArray *)getBusServiceAtBusStop:(NSString *)bus_stop_id  
{
if (!bus_stop_id)
    return nil;

NSFetchRequest *fetchrequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"BusRoute"
                                          inManagedObjectContext:[self managedObjectContext_busroutes]];
[fetchrequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"bus_stop_id == %@", bus_stop_id];
[fetchrequest setPredicate:predicate];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"bus_service_num" ascending:YES];
[fetchrequest setSortDescriptors:@[sortDescriptor]];

NSError *error = nil;
NSArray *arr = [[self managedObjectContext_busroutes] executeFetchRequest:fetchrequest error:&error];

if (error)
{
    DLog(@"Error: %@", [error localizedDescription]);

    return nil;
}

if ([arr count] <= 0)
{
    DLog(@"Empty array");

    return nil;
}

NSMutableArray *mutated = [NSMutableArray array];
NSMutableArray *reference = [NSMutableArray array];
for (BusRoute *busroute in arr)
{
    NSString *bus_service_num = busroute.bus_service_num;
    if ([reference containsObject:bus_service_num])
        continue;

    [reference addObject:bus_service_num];
    [mutated addObject:@{@"bus_service_num": bus_service_num, @"bus_type": busroute.bus_type}];
}

return [NSArray arrayWithArray:mutated];
}
0

There are 0 answers