I'm posting object with below code
Models
@interface Event : NSManagedObject
@property (nonatomic, retain) NSString * eventID;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) Calendar *calendar; // many-to-one
@interface Calendar : NSManagedObject
@property (nonatomic, retain) NSString * calendarID;
@property (nonatomic, retain) NSSet *events; // one-to-many
RKRequestDescriptor
RKObjectMapping *calendarRequestMapping = [RKObjectMapping requestMapping];
[calendarRequestMapping addAttributeMappingsFromDictionary:@{
@"calendarID":@"calendar"}];
RKObjectMapping *postRequestMapping = [RKObjectMapping requestMapping];
[postRequestMapping addAttributeMappingsFromArray:@[ @"title"]];
[postRequestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"calendar" toKeyPath:@"calendar" withMapping:calendarRequestMapping]];
RKRequestDescriptor * eventRequestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:postRequestMapping objectClass:[Event class] rootKeyPath:nil method:RKRequestMethodPOST];
[objectManager addRequestDescriptor:eventRequestDescriptor];
POST
[[RKObjectManager sharedManager] postObject:event path:@"/events/" parameters:nil success:nil failure:nil];
According to the server log, What I posted is
{u'calendar[calendar]': [u'3b60a22c-d46e-46ca-b978-ec81a8b47fcb'], u'title': [u'xxxxx']}
What I expect is
{u'calendar': [u'3b60a22c-d46e-46ca-b978-ec81a8b47fcb'], u'title': [u'xxxxx']}
I have tried
[calendarRequestMapping addAttributeMappingsFromDictionary:@{
@"calendarID":nil}];
Or
[postRequestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"calendar" toKeyPath:nil withMapping:calendarRequestMapping]];
Both of them will throw an exception.
I just fixed this issue with below mapping
RKObjectMapping *eventRequestMapping = [RKObjectMapping requestMapping];
[eventRequestMapping addAttributeMappingsFromDictionary:@{
@"title":@"title",
@"calendar.calendarID":@"calendar"}];