problem with accessing attributes of relationship after fetching with setPropertiesToFetch
I'm only fetching some attributes and a 1-to-1 relationship of an entity using -setPropertiesToFetch and result type set to NSDictionaryResultType. Now I have a problem accessing the attributes of the returned relationship. As soon as I want to access a property I get an NSInvalidArgumentException', reason: unrecognized selector sent to instance
Here is the full Exception:
2011-03-23 11:02:10.435 ThurboApp[32996:207] -[_NSObjectID_48_0 lon]: unrecognized selector sent to instance 0x5a3f490
2011-03-23 11:02:10.441 ThurboApp[32996:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSObjectID_48_0 lon]: unrecognized selector sent to instance 0x5a3f490'
Here is the corresponding source code:
- (void)fetchAllPois
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"POI" inManagedObjectContext:self.managedObjectContext];
request.entity = entity;
[request setResultType:NSDictionaryResultType];
[request setPropertiesToFetch:[NSArray arrayWithObjects:@"poiId",@"categoryId",@"poiTitle",@"coordinates", nil]];
request.sortDescriptors = nil;
request.predicate = nil;
NSError *error = nil;
self.fetchResult = [self.managedObjectContext executeFetchRequest:request error:&error];
}
- (MapPoint *)createMapPointFromDictionary:(NSDictionary *)dict
{
NSString *poiId = [dict objectForKey:@"poiId"];
NSString *title = [dict objectForKey:@"poiTitle"];
Coordinates *coords = (Coordinates *)[dict objectForKey:@"coordinates"];
NSNumber *category开发者_Python百科 = [dict objectForKey:@"categoryId"];
CLLocationCoordinate2D coordinates = CLLocationCoordinate2DMake([coords.lat doubleValue], [coords.lon doubleValue]); //here raises the exception
MapPoint *p = [[[MapPoint alloc] initWithPoiId:poiId title:title category:[category intValue] coordinates:coordinates] autorelease];
return p;
}
interface for Coordinates:
@interface Coordinates : NSManagedObject
{
}
@property (nonatomic, retain) NSNumber * lat;
@property (nonatomic, retain) NSNumber * alt;
@property (nonatomic, retain) NSNumber * lon;
@property (nonatomic, retain) NSManagedObject * poi;
@end
I've already checked, that the values returned in the dictionaries are correct. Even coordinates points to a Coordinate object.
Help is much appreciated.
Ok I managed to solve the problem. What you get for the relationship is a NSManagedObjectID which you can use with objectWithID to get the Entity itself. Here is the updated code:
- (MapPoint *)createMapPointFromDictionary:(NSDictionary *)dict
{
NSString *poiId = [dict objectForKey:@"poiId"];
NSString *title = [dict objectForKey:@"poiTitle"];
NSManagedObjectID *coordsID = (NSManagedObjectID *)[dict objectForKey:@"coordinates"]; //get the objectid
NSNumber *category = [dict objectForKey:@"categoryId"];
Coordinates *coords = (Coordinates *)[self.managedObjectContext objectWithID:coordsID]; //get the actual managedobject
CLLocationCoordinate2D coordinates = CLLocationCoordinate2DMake([coords.lat doubleValue], [coords.lon doubleValue]);
MapPoint *p = [[[MapPoint alloc] initWithPoiId:poiId title:title category:category coordinates:coordinates] autorelease];
return p;
}
精彩评论