views:

41

answers:

2

Hi all,

What is the best way to store an MKCoordinateRegion using core data? I assume there is a way to convert this to binary data somehow??

Many thanks

Jules

A: 

Sorry.... I just found the transformable type in core data, I believe this should do the trick.

Jules
Ok it turns out that I wasnt able to do this. When I tried to set the attribute I got an error telling me it was an incompatible type. What is the correct way to store an MKCoordinateRegion in Core Data, is this even allowed?Many thanks for any helpJules
Jules
A: 

My suggestion is to simply store the MKCoordinateRegion's struct values (latitude, longitude, latitudeDelta, and longitudeDelta) as separate properties, and then on your model class provide a custom accessor that assembles these into a MKCoordinateRegion object.

For example:

// Snip...boilerplate CoreData code goes here...
@dynamic latitude;
@dynamic longitude;
@dynamic latitudeDelta;
@dynamic longitudeDelta;

- (MKCoordinateRegion)region {
    CLLocationCoordinate2D center = {
        [self.latitude floatValue],
        [self.longitude floatValue]
    };
    MKCoordinateSpan span = {
        [self.latitudeDelta floatValue],
        [self.longitudeDelta floatValue]
    };

    return MKCoordinateRegionMake(center, span);
}

If you want to be extra clever, you can create a custom read-only property that exposes the internal data as above.

Michael Nachbaur