开发者

Saving CLLocation location points into an array gives me memory/performance problems

I'm having a bit of a problem with an iPhone GPS 'runkeeper'- type app. In almost all ways the app works fine, the way I've designed it is to add the CLLocation objects into an array, and then store that array of objects in a MySQL database. Then, when I open up a mapview, I simply pull the array from the server and, with a little manipulation, use the data to add pins to the map.

Like I say, this has all been working fine, and works beautifully well on the simulator and on the device when sitting at my desk (and, btw, using instruments - no leaks). However, when I've tested the app on the device and actually left the house and walked 15 mins down the street, the app crashes at the point the CLLocationManager object stops updating and the array is POSTed to MySQL - almost certainly (I think) because of memory problems.

That being the case, and given I'm a relative noob, so be gentle, I'm looking for advice around developing a more efficient way of storing the objects into an array so that the probably huge amount of then stored location dat doesn't melt the app.

Here's (a selection) of the code I'm using:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {

//note I have taken out the code determining whether CLocation object is valid, and one or two other little things as well

    if (recording == YES) {//the start button has been pressed but the stop button hasn't.


        if (alreadyBeenRound == NO) {

            if (firstRecord == YES) {

                //create the pointsonRoute array
                if (pointsOnRouteExists == NO) {

                    pointsOnRoute = [[NSMutableArray alloc] init];
                    pointsOnRouteExists = YES;
                }

                [pointsOnRoute removeAllObjects];

                firstRecord = NO; //after setting first record, can move on

            }

            [pointsOnRoute addObject:newLocation];

                            //more stuff taken out here

            alreadyBeenRound = YES;

        } else { 

            [pointsOnRoute addObject:newLocat开发者_运维问答ion];


        }

//I do release the array


Just a wild guess. When you sit at your desk the updates stop after a short while since your location is not changing (depending on how you set up the location manager). So you don't get so many points in your array. When you move you may get a lot of them. You could check when you are at your desk how many inserts in your array you get and how many while moving...


You have to store only major updates. Lets say you will be storing every every 5 meter difference on the first kilometre (in case the user goes just for a short walk) and than every 20-50 meters if they went for a run. You can also trim some close values or values in the same direction on a background thread shall your array/CoreData start growing significantly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜