POST请求的响应对象在尝试映射到对象时崩溃。打印以下输出“CoreData:错误:无法在NSManagedObject类映射操作上调用指定的初始化程序”
正如消息所示,正在使用RKObjectRequestOperation而不是RKManagedObjectRequestOperation。我的问题可能很复杂,因为外部对象是一个普通的nsobject,但在其中有一个包含nsmanagedObject实体的成员变量。我一直在探索代码,我不知道如何将代码引导到我想要的方向。更令人痛苦的是,我能够映射到GET请求的响应对象。响应对象与异常基本相同。这是我正在使用的代码
我的响应对象是一个普通的NSObject,其中包含一个嵌套的托管对象。
// response definition
@interface PDResponse : NSObject
@property (nonatomic, strong) NSString *message;
@property (nonatomic, strong) NSNumber *success;
@property (nonatomic, strong) id value;
@property (nonatomic, strong) NSDictionary *exception;
@property (nonatomic, strong) NSString *transactionId;
@end
// Mapping for the download object
RKEntityMapping * repMapping = [RKEntityMapping mappingForEntityForName:@"PDRepresentative" inManagedObjectStore:managedObjectStore];
repMapping = @[@"repId"];
[repMapping addAttributeMappingsFromDictionary:@{
@"RepId": @"repId",
@"First": @"firstName",
@"Middle": @"middleName",
@"Last": @"lastName",
@"Address1": @"address1",
@"Address2": @"address2",
@"City": @"city",
@"Zip": @"zip",
@"Country": @"country",
@"Company": @"company",
@"Phone": @"phone",
@"PhonePersonal": @"phonePersonal",
@"Fax": @"fax",
@"Email": @"email",
@"EventId": @"event.eventId",
@"Specialty": @"specialty"}];
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[PDResponse class]];
[responseMapping addAttributeMappingsFromDictionary:@{@"Message": @"message",
@"Success": @"success",
@"Ex": @"exception",
@"TransactionId": @"transactionId"}];
[responseMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Value"
toKeyPath:@"value"
withMapping:repMapping]];
[objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping: responseMapping
method:RKRequestMethodPOST
pathPattern:@"PostAddOrUpdateRep/"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];
// POST Object
RKObjectManager *objectManager = [RKObjectManager sharedManager];
NSMutableURLRequest *request = [objectManager requestWithObject:self method:RKRequestMethodPOST path:@"PostAddOrUpdateRep/" parameters:nil];
RKObjectRequestOperation *operation = [objectManager objectRequestOperationWithRequest:request
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult){
PDResponse *response = mappingResult.firstObject;
response = [response isKindOfClass:[PDResponse class]] ? response : nil;
TRACE_LOG(@"%@", response);
}
failure:^(RKObjectRequestOperation *operation, NSError *error){
ERROR_LOG(@"Failure int %s %@, %@", __PRETTY_FUNCTION__, operation, error);
}];
[objectManager enqueueObjectRequestOperation:operation];
self.sent = @(YES);
我观察了很多其他帖子,并根据我在这些帖子中找到的信息完成了以下操作。
有人建议在另一篇文章中删除和添加斜杠,因为描述符无法正确映射到路径,但事实并非如此。我选择不使用postObject:path:参数:success:failure函数,因为它给我一个错误,用于映射目标对象变量被分配到PDRepresentative类型而不是PDResponse的响应。
我也能正确上传,所以我的managedObjectStore很好。正如您所看到的,我正在使用RKEntityMapping对象,因此,人们希望这会告诉代码使用NSManagedObject而不是NSObject。
名为Wain的用户在另一篇文章中建议混合托管和非托管项目可能会造成潜在问题。我倾向于相信他,但我想通过这个,因为我确实通过我的GET请求让我的回复工作。
非常感谢任何帮助。谢谢。
答案 0 :(得分:2)
我很愚蠢,只是将创建请求操作的函数更改为managedObjectRequestOperationWithRequest:managedObjectContext:success:fail
而不是使用objectRequestOperationWithRequest:success:fail
。我只是阅读了我在问题的第一段写的内容。
上帝我是个白痴。