我曾经多次使用RestKit进行对象映射,但这是第一次使用核心数据对象。
我将嵌套的json内容映射到两个核心数据实体。这会导致验证错误 - 映射失败。
为什么关系失败的任何建议?
E restkit.object_mapping:RKMappingOperation.m:335 Validation failed while mapping attribute at key path 'workflowSteps' to value {(
<NUWorkflowStepMO: 0xe717a00> (entity: WorkflowSteps; id: 0xe717a40 <x-coredata:///WorkflowSteps/t150ED0DB-CADD-46FD-804F-1F2094B7C7DF194> ; data: {
operatingCompany = nil;
"operatingCompany_id" = 475;
position = 2;
step = SecurityCheck;
uid = 222;
}),
<NUWorkflowStepMO: 0xe88c1a0> (entity: WorkflowSteps; id: 0xe8e5a50 <x-coredata:///WorkflowSteps/t150ED0DB-CADD-46FD-804F-1F2094B7C7DF195> ; data: {
operatingCompany = nil;
"operatingCompany_id" = 475;
position = 3;
step = "Mission Complete";
uid = 333;
}),
<NUWorkflowStepMO: 0xe7194b0> (entity: WorkflowSteps; id: 0xe7194a0 <x-coredata:///WorkflowSteps/t150ED0DB-CADD-46FD-804F-1F2094B7C7DF193> ; data: {
operatingCompany = nil;
"operatingCompany_id" = 475;
position = 1;
step = Welcome;
uid = 111;
})
)}. Error: The operation couldn’t be completed. (Cocoa error 1550.)
2014-07-24 14:39:23.868 NuBON-Dev[43457:60b] E restkit:RKLog.m:151 Validation Error
NSLocalizedDescriptionKey: The operation couldn’t be completed. (Cocoa error 1550.)
NSValidationKeyErrorKey: workflowSteps
NSValidationPredicateErrorKey: (null)
NSValidationObjectErrorKey:
<NUOperatingCompanyMO: 0xe9c3be0> (entity: OperatingCompany; id: 0xe9c3c20 <x- coredata:///OperatingCompany/t150ED0DB-CADD-46FD-804F-1F2094B7C7DF192> ; data: {
categoryId = 0;
workflowSteps = (
);
uid = 475;
})
这是json:
{
"operatingCompanyIds":[
475
],
"itemCount":1,
"items":[
{
"id":475,
"workflowStep":[
{
"id":111,
"operatingCompany_id":475,
"step":"Welcome",
"position":1
},
{
"id":222,
"operatingCompany_id":475,
"step":"SecurityCheck",
"position":2
},
{
"id":333,
"operatingCompany_id":475,
"step":"Mission Complete",
"position":3
}
],
"categoryId":3,
}
]
}
我有两个实体的核心数据模型&#34; item&#34;和&#34; workflowStep&#34;,item与workflowSteps有多对多的关系。
这是我的映射:
OperatingCompany:
+ (NSDictionary*) mappingDictionary {
return @{
@"id": @"uid",
@"name": @"name",
@"category_id": @"category_id",
};
}
+ (RKEntityMapping*) entityMappingForManagedObjectStore:(RKManagedObjectStore *)managedObjectStore {
RKEntityMapping* mapping = [RKEntityMapping mappingForEntityForName:[NUOperatingCompanyMO entityName] inManagedObjectStore:managedObjectStore];
[mapping setIdentificationAttributes:@[@"uid"]];
[mapping addAttributeMappingsFromDictionary:[NUOperatingCompanyMO mappingDictionary]];
RKRelationshipMapping* relationshipMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"workflowStep" toKeyPath:@"workflowSteps" withMapping:[NUCheckoutWorkStepMO entityMappingForManagedObjectStore:managedObjectStore]];
[mapping addPropertyMapping:relationshipMapping];
}
WorkflowStep
+ (NSDictionary*) mappingDictionary {
return @{
@"id": @"uid",
@"operatingCompany_id": @"operatingCompany_id",
@"step": @"step",
@"position": @"position"
};
}
+ (RKEntityMapping*) entityMappingForManagedObjectStore:(RKManagedObjectStore *)managedObjectStore {
RKEntityMapping* mapping = [RKEntityMapping mappingForEntityForName:[NUCheckoutWorkStepMO entityName] inManagedObjectStore:managedObjectStore];
[mapping setIdentificationAttributes:@[@"uid"]];
[mapping addAttributeMappingsFromDictionary:[NUCheckoutWorkStepMO mappingDictionary]];
[mapping addConnectionForRelationship:@"operatingCompany" connectedBy:@"uid"];
return mapping;
}