Rob E.
Rob E.

Reputation: 194

Storing latitude/longitude in Core Data

I have an NSManagedObject that has two double properties for latitude and longitude. When creating the NSManagedObject subclass I check the "Use scalar values" checkbox. The .h file declared the properties as this:

@property (nonatomic) double latitude;
@property (nonatomic) double longitude;

I am retrieving a latitude and longitude value from a web service and am setting them locally with the following:

double tempLatitude = [[location objectForKey:@"lat"] doubleValue];
double tempLongitude = [[location objectForKey:@"lng"] doubleValue];

However, the application blows up with an EXC_BAD_ACCESS when I try to use the dynamically-created setter from the NSManagedObject subclass.

[newPlace setLatitude:tempLatitude];

I also tried to convert the tempLatitude and tempLongitude to NSNumber values, but the setter tells me that I'm "Sending 'NSNumber *__strong' to paramter of incompatible type 'double'"

How can I correctly transform the values I'm retrieving for storage in my Core Data object? Or do I have create some kind of custom setter in my NSManagedObject subclass.

If this is a repeat question I apologize. I've searched quite a bit and most of the answers stop at "just store lat/long as doubles" without going into detail about that.

EDIT More code was requested. I've got it spread out over a few files, but I consolidated a simple request down into my AppDelegate just for testing and it still blows up with EXC_BAD_ACCESS when I try to run this chunk.

NOTE: I'm just trying to keep an NSManagedObject in memory for now (this context is not pointing to the database.)

Here's my managed object header file:

Place.h

@interface Place : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic) double latitude;
@property (nonatomic) double longitude;

@end

Place.m

@implementation Place

@dynamic name;
@dynamic latitude;
@dynamic longitude;

@end

Here's the lump of code that is setting up the context, creating an instance of the subclass, and trying to write a double value to it:

NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];

NSPersistentStoreCoordinator *memPsc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];

NSError *error = nil;

if (![memPsc addPersistentStoreWithType:NSInMemoryStoreType
                          configuration:nil
                                    URL:nil
                                options:nil
                                  error:&error])
{
    [NSException raise:@"Open failed!" format:@"Reason: %@", [error localizedDescription]];
}

NSManagedObjectContext *memContext = [[NSManagedObjectContext alloc] init];

[memContext setPersistentStoreCoordinator:memPsc];

Place *place = [NSEntityDescription insertNewObjectForEntityForName:@"Place" inManagedObjectContext:memContext];

double tempLatitude = [@"1.23" doubleValue];
double tempLongitude = [@"3.456" doubleValue];

[place setLatitude:tempLatitude];  // BLOWS UP HERE: EXC_BAD_ACCESS
[place setLongitude:tempLongitude];

Upvotes: 1

Views: 1148

Answers (2)

Mohammad Rana
Mohammad Rana

Reputation: 173

    NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Contacts"     inManagedObjectContext:context];

NSNumber *longitudesNumber = [NSNumber numberWithDouble:longitudes];
NSNumber *latitudesNumber = [NSNumber numberWithDouble:latitudes];
[newDevice setValue: longitudesNumber forKey:@"longitude"];
[newDevice setValue: latitudesNumber   forKey:@"latitude"];

Upvotes: 0

Rob E.
Rob E.

Reputation: 194

I ended up just changing the properties of the NSManagedObject subclass to NSNumber and assigning NSNumber values to the objects. It appears to have worked. I wish I knew the "right" way to do it, if this isn't.

Upvotes: 0

Related Questions