marciokoko
marciokoko

Reputation: 4986

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?

Upvotes: 1

Views: 85

Answers (2)

Wain
Wain

Reputation: 119031

Look at using a batched fetch request. You can't display all of the 84+ items to the user at the same time so you don't need to pull them all out of the data store at the same time. The fetch request can be configured to sort the items and to return pages suitable for the number of items that can be seen at any one time. Then each time a page is loaded there will be a very small amount of processing / conversion but the overall cost will be distributed. Also, if the user never 'scrolls' to see data then it won't be loaded.

Upvotes: 1

MasterPlanMan
MasterPlanMan

Reputation: 1032

The best would be to use threads to deal with the data faster. Also, it would give you the opportunity to give some feedback to the user making the lag almost unnoticeable or at least less annoying - especially if you consider sending data even if all is not processed yet.

If you're looking for code optimizations, I would suggest that you use a profiler and modify the code accordingly.

Upvotes: 1

Related Questions