Post RestKit 中的对象错误
Post object error in RestKit
我正在使用以下代码发布对象
型号
@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];
根据服务器日志,我发布的是
{u'calendar[calendar]': [u'3b60a22c-d46e-46ca-b978-ec81a8b47fcb'], u'title': [u'xxxxx']}
我期待的是
{u'calendar': [u'3b60a22c-d46e-46ca-b978-ec81a8b47fcb'], u'title': [u'xxxxx']}
我试过了
[calendarRequestMapping addAttributeMappingsFromDictionary:@{
@"calendarID":nil}];
或
[postRequestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"calendar" toKeyPath:nil withMapping:calendarRequestMapping]];
两者都会抛出异常
我刚刚用下面的映射解决了这个问题
RKObjectMapping *eventRequestMapping = [RKObjectMapping requestMapping];
[eventRequestMapping addAttributeMappingsFromDictionary:@{
@"title":@"title",
@"calendar.calendarID":@"calendar"}];
我正在使用以下代码发布对象
型号
@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];
根据服务器日志,我发布的是
{u'calendar[calendar]': [u'3b60a22c-d46e-46ca-b978-ec81a8b47fcb'], u'title': [u'xxxxx']}
我期待的是
{u'calendar': [u'3b60a22c-d46e-46ca-b978-ec81a8b47fcb'], u'title': [u'xxxxx']}
我试过了
[calendarRequestMapping addAttributeMappingsFromDictionary:@{
@"calendarID":nil}];
或
[postRequestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"calendar" toKeyPath:nil withMapping:calendarRequestMapping]];
两者都会抛出异常
我刚刚用下面的映射解决了这个问题
RKObjectMapping *eventRequestMapping = [RKObjectMapping requestMapping];
[eventRequestMapping addAttributeMappingsFromDictionary:@{
@"title":@"title",
@"calendar.calendarID":@"calendar"}];