Thursday, 15 August 2013

How can I improve this performance?

How can I improve this performance?

I have a UITabBarController based app that:
1) Fetches data from web and parses it into CD (this works fine). [In Tab
1] 2) Then when second tab [In Tab 2] is selected, it runs this method:
{ viewDidLoad > loadRecordsFromCD (performBlockAndWait) >
populateLocationsToSort }
- (void)populateLocationsToSort {
//1. Get UserLocation based on mapview
self.userLocation = [[CLLocation alloc]
initWithLatitude:self.userLocation.coordinate.latitude
longitude:self.userLocation.coordinate.longitude];
// Loop thru dictionary-->Create locations
// 2. Loop thru dictionary to get Custom Objects
for (Location * locationObject in self.farSiman) {
// 3. Unload objects values into locals
//PARSE ALL DATA
NSString *coordenadas = locationObject.coordenadas;
NSArray *coordinatesArray = [coordenadas
componentsSeparatedByString:@","];
NSString * latitude = [coordinatesArray objectAtIndex:0];
NSString * longitude = [coordinatesArray objectAtIndex:1];
NSString * storeDescription = locationObject.nombrePublico;
NSString * address = locationObject.direccion;
NSString * ciudad = locationObject.ciudad;
NSString * horario = locationObject.horario;
NSString * hor_LV = locationObject.hor_LV;
NSString * hor_S = locationObject.hor_S;
NSString * hor_D = locationObject.hor_D;
NSString * telefono = locationObject.telefono;
NSString * celular_TA = locationObject.celular_TA;
NSString * celular_TB = locationObject.celular_TB;
NSString * hrs24 = locationObject.hrs24;
NSString * driveThru = locationObject.driveThru;
//NSString * estado = locationObject.estado;
NSString * estado;
// IF self.open24hrs SELECTED
if (self.open24hrs) {
// Set it based on TimeComparator
if ([TimeComparator
dealWithTimeStrings2:locationObject.hor_LV]) {
estado = @"Abierta";
} else {
estado = @"Cerrada";
}
} else {
estado = locationObject.estado;
}
// 4. Create MyLocation object based on locals gotten from Custom
Object
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude.doubleValue;
coordinate.longitude = longitude.doubleValue;
MyLocation *annotation = [[MyLocation alloc]
initWithName:storeDescription address:address
coordinate:coordinate distance:0 ciudad:ciudad horario:horario
telefono:telefono hrs24:hrs24 driveThru:driveThru hor_LV:hor_LV
hor_D:hor_D hor_S:hor_S celular_TA:celular_TA
celular_TB:celular_TB estado:estado];
// 5. Calculate distance between locations & uL
CLLocation *pinLocation = [[CLLocation alloc]
initWithLatitude:annotation.coordinate.latitude
longitude:annotation.coordinate.longitude];
CLLocationDistance calculatedDistance = [pinLocation
distanceFromLocation:self.userLocation];
annotation.distance = calculatedDistance/1000;
//Add annotation to local NSMArray
[self.annotationsToSort addObject:annotation];
} //ENDS FOR LOOP
//SORT the created annotationsToSort
[self sort];
}
It takes the array populated from the CD fetch and creates objects out of
them. It creates new objects because it must take the hor_LV field, parse
it into dates and compare them to now in order to determine the
location.estado.
Currently there are 84 records being fetched and I can already notice a
lag from the time i tap on the second Tab, (this tableviewcontroller), and
the time it actually displays onscreen.
I can't pre-parse this array because the user sets some filters on Tab 1
which are passed in to Tab 2 before the data is fetched from the database.
So I know that the fetch must occur as Tab 2 loads. My question is, what
could I do to speed this up or not let the lag be so obvious?

No comments:

Post a Comment