Flixel Forums

development => iOS => Topic started by: juanmunhoesjunior on Tue, Sep 25, 2012

Title: Memory Warning
Post by: juanmunhoesjunior on Tue, Sep 25, 2012
Hi guys i have a problem in my app with memory warning..

I have a big database with fields: coordinates (latitude, longitude) and distance.I need to calculate the distance between theses coordinates. (CalculateDistance)
I also have a class to store in a object. It calls Location.

the nsmutablearray listOfObjects is where i save my database (in memory i dont know if is the best way), to order this list by distance.

I use NSComparisonResult to compare 2 distances to call sortUsingSelector:@selector(OrderByDistance:)
OrderByDistance is a method implemented in class Location:

@implementation Location

-(NSComparisonResult) OrderByDistance:(Location *) other


    return [[self distance] compare:[other distance]];



i have this algorithm  :

- (void) CalculateDistance {


    NSMutableArray * copyListOfItems = [[NSMutableArray alloc]init];


    double dist,lat,longi,radius = 6371,dLat,dLon,a,c,d;

    for(Location * objeto in listOfObjects)


        dist = [objeto.distance doubleValue];

        lat = [objeto.latitude doubleValue];

        longi = [objeto.longitude doubleValue];


        //Calculo p/ distancia em km

        dLat = (CurrentLatitude - lat) * M_PI/180;

        dLon = (CurrentLongitude - longi) * 3.14/180;

        a = sin(dLat/2) * sin(dLat/2) + cos((lat * M_PI)/180) * cos((CurrentLatitude * M_PI)/180) * sin(dLon/2) * sin(dLon/2);

        c = 2 * atan2(sqrt(a), sqrt(1-a));

        d = (radius * c);

        dist = d;


        NSDecimalNumber *ds = [[NSDecimalNumber alloc]initWithDouble:dist];

        objeto.distance = ds;


        [copyListOfItems addObject:objeto];



    //Order my list

    [copyListOfItems sortUsingSelector:@selector(OrderByDistance:)];

    pickerList = [[NSMutableArray alloc]init];

    pickerList = [copyListOfItems mutableCopy];


is this the best way to do it or have something better?