gpt4 book ai didi

ios - RestKit 对象映射 POST 响应, "CoreData: error: Failed to call designated initializer on NSManagedObject class mapping operation"

转载 作者:行者123 更新时间:2023-11-29 10:49:41 27 4
gpt4 key购买 nike

尝试映射到对象时,POST 请求的响应对象崩溃。打印以下输出“CoreData: error: Failed to call designated initializer on NSManagedObject class mapping operation”

消息表明正在使用 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:parameters:success:failure 函数,因为它给我一个错误,用于映射目标对象变量分配给 PDRepresentative 而不是 PDResponse 类型的响应。

我也能够正确上传,所以我的 managedObjectStore 没问题。如您所见,我正在使用 RKEntityMapping 对象,因此,人们希望这会告诉代码使用 NSManagedObject 而不是 NSObject。

一位名叫 Wain 的用户在另一篇帖子中建议,混合托管和非托管项目可能会带来潜在问题。我倾向于相信他,但我想解决这个问题,因为我确实让它对我的 GET 请求的响应起作用。

如有任何帮助,我们将不胜感激。

最佳答案

我很愚蠢,只是将创建请求操作的函数更改为 managedObjectRequestOperationWithRequest:managedObjectContext:success:fail 而不是使用 objectRequestOperationWithRequest:success:fail。我只是阅读了我在问题的第一段中写的内容。

天哪,我是个白痴。

关于ios - RestKit 对象映射 POST 响应, "CoreData: error: Failed to call designated initializer on NSManagedObject class mapping operation",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21008880/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com