Khledon
Khledon

Reputation: 183

Saving a location Array to NSUserDefaults

I'm attempting to build an app which will track a route, then store the route in parse.com so I can overlay the route taken by a user using MKpolyline.

I'm very new to Objective-c and IOS development, so please excuse my ignorance!!

I'm stuck when I try to save the route taken, then send/save the location array so I can rebuild the MKpolyline on the next view controller which is opened when the user completes the activity.

I'm not sure whether to save the location array to NSUserDefaults or save it to core data. At the moment I am converting the Array to an NSValue and the saving it to NSUserDefaults like so:

 count = [self.locations count];

        CLLocationCoordinate2D coordinates[count];
        for (NSInteger i = 0; i < count; i++) {
            coordinates[i] = [(CLLocation *)self.locations[i] coordinate];
            NSValue *locationValue = [NSValue valueWithMKCoordinate:coordinates[i]];
            [_locationsArray addObject:locationValue];




 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject:totalDistance forKey:@"totalDistance"];
    [defaults setObject:_locationsArray forKey:@"mapOverlay"];
   // [defaults setDouble:_totalTime forKey:@"totalTime"];
    [defaults setObject:avgSpeedToBeSaved forKey:@"averageSpeed"];
    [defaults setObject:totalCalories forKey:@"totalCalories"];

    [defaults synchronize];

Is this the right way to do this? And how do I rebuild the locations Array.

If anyone could point me in the right direction it would be greatly appreciated.

I've now changed my code to what was suggested by manecosta to rebuild the CLLocationCoordinates to create an MKPolyline, but my issue now is that the array is Null from where I start to convert into an NSValue. I am unable to figure out why this is, is there something wrong with the way I'm building LocationsArray in the first place?

Upvotes: 0

Views: 1051

Answers (2)

Akshit Zaveri
Akshit Zaveri

Reputation: 4244

You can get object from NSUserDefaults by using objectForKey:

As you have [defaults setObject:totalDistance forKey:@"totalDistance"];.

Now if you want to get the totalDistance back from the NSUserDefaults` then you can use following code:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *arrayLocation = [defaults objectForKey:@"totalDistance"];

But a piece of advise for you is not to use NSUserDefaults for your project. NSUserDefaults should only be used to store little amount of data (It's just a personal opinion.). Your locationArray is going to be quite large. Right?

A nice comparison between the two options NSUserDefaults & CoreData What are the limitations of NSUserDefaults?

I'm posting answer here (from above link - in case the question gets unavailable in future time - who knows !!)

NSUserDefaults offers a trivial learning curve and thread safe implementation.

Otherwise I've found Core Data superior in every way. Especially with regards to configuring default values and migration routines.

EDIT: I actually haven't tried but it seems to be this way.. Tell me if it does not work.

To get back NSArray from NSValue

NSValue *value = [defaults objectForKey:@"mapOverlay"];
NSArray *arrayTemp;
[value getValue:&arrayTemp];

Upvotes: 0

manecosta
manecosta

Reputation: 8772

Yes, I guess you're doing it right and to rebuild just do the opposite, which should be something like:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSArray *locationsArray = [defaults objectForKey:@"mapOverlay"];

CLLocationCoordinate2D coordinates[[locationsArray count]];
NSInteger i = 0;
for(NSValue *locationValue in locationsArray){
    coordinates[i] = [locationValue MKCoordinateValue];
    i++;
}

About the fact that you're using User Defaults to store tons of data. I don't really know what is correct, but I'll tell you that I've previously used it to store the cache of my app which were quite big arrays and dictionaries and it never failed me.

Upvotes: 1

Related Questions