使用RestKit进行POST后导致nil对象被发布

Nun*_*ter 4 iphone core-data ipad ios restkit

我在使用RestKit 0.20pre6发布NSManagedObject时遇到问题,导致在我的Rails站点上使用nil值创建一个条目.我怀疑是mapper,但我已经正确地设置了我的RK对象.很少有例子和有限的支持.该示例处理NSObjects而非CoreData,因此我不得不做出一些假设.

这是我的发布代码,然后是控制台输出.该应用程序从服务器获取对象很好.

        // Sync the added object to the server
    // ****************************************

    RKObjecMapping *responseMapping = [RKObjectMapping mappingForClass:[Event class]];
//  RKEntityMapping *responseMapping = [RKEntityMapping mappingForEntityForName:@"Event" inManagedObjectStore:self.managedObjectStore];
    [responseMapping addAttributeMappingsFromDictionary:@{
     @"id": @"eventId",
     @"name": @"name",
     @"amount": @"amount",
     @"length": @"length",
     @"updated_at": @"updatedAt",
     @"created_at": @"createdAt",
     @"latitude": @"latitude",
     @"longitude": @"longitude",
     @"thumbnail": @"thumbnail",
     @"airTemp": @"airTemp",
     }];

    NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
    RKResponseDescriptor *eventDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
                                                                                    pathPattern:@"/events"
                                                                                        keyPath:@"events"
                                                                                    statusCodes:statusCodes
                                                                                                ];

    RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; // objectClass == NSMutableDictionary

    [requestMapping addAttributeMappingsFromDictionary:@{
     @"id": @"eventId",
     @"name": @"name",
     @"amount": @"amount",
     @"length": @"length",
     @"updated_at": @"updatedAt",
     @"created_at": @"createdAt",
     @"latitude": @"latitude",
     @"longitude": @"longitude",
     @"thumbnail": @"thumbnail",
     @"airTemp": @"airTemp",
     }];


    // For any object of class Article, serialize into an NSMutableDictionary using the given mapping and nest
    // under the 'article' key path
    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
                                                                                   objectClass:[Event class]
                                                                                   rootKeyPath:@"events"
                                                                                                ];

    LocationsAppDelegate *appDelegate = (LocationsAppDelegate *)[[UIApplication sharedApplication] delegate];
//  RKObjectManager *manager = [RKObjectManager managerWithBaseURL:gBaseURL];


    [RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;
    [[RKObjectManager sharedManager] addRequestDescriptor:requestDescriptor];
    [[RKObjectManager sharedManager] addResponseDescriptor:eventDescriptor];

    NSLog(@"RVC-addEvent %@ - %@", appDelegate.user.login, appDelegate.user.password);
    [[NBEventManager sharedManager] setUsername:appDelegate.user.login andPassword:appDelegate.user.password];
    appDelegate.objectManager.HTTPClient = [NBEventManager sharedManager];


    event = [eventsArray objectAtIndex:0];
    [[RKObjectManager sharedManager] postObject:event
                                           path:@"/events"
                                     parameters:nil
                                        success:nil
                                        failure:nil
                                                ];

}
Run Code Online (Sandbox Code Playgroud)

2013-01-15 23:25:52.857 fish [38803:13403]我在restkit.network:RKHTTPRequestOperation.m:152 POST'http:// localhost:3000/events'2013-01-15 23:25:52.969 fish [ 38803:16903]我restkit.network:RKHTTPRequestOperation.m:179 POST'http:// localhost:3000/events'(201 Created)[0.1121 s] 2013-01-15 23:25:52.971 fish [38803:17507] D restkit.object_mapping:RKMapperOperation.m:360执行表示的映射操作:{event = {airTemp =""; amount =""; "created_at"="2013-01-16T04:25:52Z"; id = 54; latitude =""; length =""; 经度=""; name =""; thumbnail =""; "updated_at"="2013-01-16T04:25:52Z"; "user_id"=""; }; }和targetObject :( entity:Event; id:0x9c7f710; data:{airTemp = 0; amount = nil; createdAt ="2013-01-16 04:25:52 +0000"; eventId = 0; latitude ="28.158466" ; length = 0; longitude =" - 82.509215"; name = nil; photo = nil; thumbnail = nil; updatedAt = nil;})2013-01-15 23:25:52.971 fish [38803:17507] D restkit.object_mapping :RKMapperOperation.m:287在keyPath'event'找到可映射数据:{airTemp =""; amount =""; "created_at"="2013-01-16T04:25:52Z"; id = 54; latitude =""; length =""; 经度=""; name =""; thumbnail =""; "updated_at"="2013-01-16T04:25:52Z"; "user_id"=""; 2013-01-15 23:25:52.971 fish [38803:17507] D restkit.object_mapping:RKMapperOperation.m:232要求映射源对象{airTemp =""; amount =""; "created_at"="2013-01-16T04:25:52Z"; id = 54; latitude =""; length =""; 经度=""; name =""; thumbnail =""; "updated_at"="2013-01-16T04:25:52Z"; "user_id"=""; }使用映射eventId>","name>","amount>","length>","updatedAt>","createdAt>","latitude>","longitude>","airTemp>","thumbnail> ")> 2013-01-15 23:25:52.971鱼[38803:17507] D restkit.object_mapping:RKMappingOperation.m:754开始绘图操作... 2013-01-15 23:25:52.972鱼[38803:17507 ] D restkit.object_mapping:RKMappingOperation.m:818成功完成映射操作... 2013-01-15 23:25:52.973 fish [38803:17507] D restkit.object_mapping:RKMapperOperation.m:316在keyPath找到不可映射的值:事件2013-01-15 23:25:52.973 fish [38803:17507] D restkit.object_mapping:RKMapperOperation.m:389完成执行对象映射.结果:{event ="(entity:Event; id:0x9c7f710; data:{ \n airTemp = nil; \n amount = nil; \n createdAt = \"2013-01-16 04:25:52 +0000 \"; \n eventId = 54; \n latitude = nil; \n length = nil ; \n\longitude = nil; \n name = nil; \n photo = nil; \n thumbnail = nil; \n updatedAt = \"2013-01-16 04:25:52 +0000 \"; \n}) ";} 2013-01-15 23:25:52.973 f ish [38803:13403] D restkit.core_data:RKManagedObjectRequestOperation.m:534跳过删除孤立对象:仅对GET请求执行.2013-01-15 23:25:52.976 fish [38803:11f03] D restkit.core_data:RKManagedObjectRequestOperation.m:599刷新映射的目标对象(实体:事件; id:0x9c7f710;数据:{airTemp = nil; amount = nil; createdAt ="2013-01-16 04:25:52 +0000"; eventId = 54; latitude = nil; length = nil; longitude = nil; name = nil; photo = nil; thumbnail = nil; updatedAt ="2013- 01-16 04:25:52 +0000";})在上下文中

这是我的代码,因为我已将所有映射移动到应用程序委托并实现了RKEntityMapping.我仍然没有附加到POST调用的JSON对象.

    RKEntityMapping *eventMapping = [RKEntityMapping mappingForEntityForName:@"Event" inManagedObjectStore:managedObjectStore];
[eventMapping addAttributeMappingsFromDictionary:@{
 @"id": @"eventId",
 @"name": @"name",
 @"amount": @"amount",
 @"length": @"length",
 @"updated_at": @"updatedAt",
 @"created_at": @"createdAt",
 @"latitude": @"latitude",
 @"longitude": @"longitude",
 @"thumbnail": @"thumbnail",
 @"airTemp": @"airTemp",
 }];
eventMapping.identificationAttributes = @[ @"eventId" ];

//RKRelationshipMapping *eventRelationship = [RKRelationshipMapping relationshipMappingFromKeyPath:@"events"
//                                                                                     toKeyPath:@"events"
//                                                                                   withMapping:eventMapping];
//[eventMapping addPropertyMapping:eventRelationship];

// Register our mappings with the provider
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:eventMapping
                                                                                   pathPattern:nil
                                                                                       keyPath:@"event"
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];

RKEntityMapping *eventRequestMapping = [eventMapping inverseMapping];
RKRequestDescriptor *eventRequestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:eventRequestMapping
                                                                                    objectClass:[Event class]
                                                                                    rootKeyPath:@"event"
                                                                                                ];
[objectManager addRequestDescriptor:eventRequestDescriptor];
[RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;
Run Code Online (Sandbox Code Playgroud)

Ant*_*ich 5

RESTKit 0.20pre6中存在一个错误,它将HTTP Body打印到NSLog,POST请求为(null),即使它是一个对象.我在开发分支中修复了它. https://github.com/RestKit/RestKit/pull/1139