I am attempting to use a NSManagedObject as a response class in my requests, but I have relationships that I need saturated as well that I do not want managed because they change far more frequently. I have attempted to use just plain NSObject's along with transient relationships to NSManagedObject's, but either approach ends up with empty relationships (the transient or NSObjects don't get saturated).
Here is the object I want saturated from the request and stored in SQLite:
@interface Car : NSManagedObject
@property (nonatomic, retain) NSString *brand
@property (nonatomic, retain) MLocation *location //don't want persisted
@end
Here is the object I don't want persisted, but saturated:
//I have tried this with NSObject and NSManagedObject
@interface MLocation : NSObject
@property (nonatomic, assign) double latitude;
@property (nonatomic, assign) double longitude;
@end
Here is the response descriptor:
[manager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:self.buildCarMapping
method:RKRequestMethodGET
pathPattern:@"/v1/cars/near/:latitude/:longitude"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)] ];
Here is the object mapping:
- (RKObjectMapping *)buildCarMapping {
RKEntityMapping *carMapping = [RKEntityMapping mappingForEntityForName:@"Car" inManagedObjectStore:self.managedObjectStore];
[carMapping addAttributeMappingsFromDictionary:@{
@"brand" : @"brand"
}];
[carMapping addRelationshipMappingWithSourceKeyPath:@"location" mapping:self.buildLocationMapping];
return carMapping;
}
//I have tried this with NSObject and a transient NSManagedObject relationship
- (RKObjectMapping *)buildLocationMapping {
RKObjectMapping *locationMapping = [RKObjectMapping mappingForClass:[MLocation class]];
[locationMapping addAttributeMappingsFromDictionary:@{
@"longitude": @"longitude",
@"latitude": @"latitude"
}];
return locationMapping;
}
Here is an example response:
{
"brand": "Chevy",
"location": { //This is ignored by RestKit using my mappings
"latitude": 42.8,
"longitude": -98.6
}
}