Fyi,我对Restkit的API不太熟悉。我更喜欢将json反序列化为域对象的其他方法;但是,我继承了一个项目,这是修复bug的最快解决方案。
我正在尝试将下面json中的“作业”映射到“doctorAssignments”,这是核心数据中的1-Many关系。
示例json:
{
"id": 43,
"time_zone": "Mountain Time (US & Canada)",
"accepting_patients": true,
"auto_set_context_id": 44,
"audio_notifications": false,
"assignments": [
{
"id": 14,
"active_since": "2015-06-17T23:26:03Z",
"doctor": {
"id": 44,
"name": "Darth Vader PsyD",
"title": "Dr.",
"bio": "",
"type": "Doctor",
"profile_image_url": "https://cirrusmd-staging.s3.amazonaws.com/uploads/doctor/profile_image/43/darth.png?X-Amz-Expires=600&X-Amz-Date=20150618T163351Z&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJGWZJVKLZCGMXJ2Q/20150618/us-east-1/s3/aws4_request&X-Amz-SignedHeaders=host&X-Amz-Signature=b0bc0163f4cf8c3c73572198c596565b0012a33331936baf4b14b9a255c0870f",
"credential_id": 214
}
}
],
"practice": {
"id": 2,
"name": "Test Practice",
"address": "",
"phone": "",
"fax": "",
"created_at": "2013-06-11T17:49:48Z",
"updated_at": "2013-06-11T17:49:48Z",
"email": "",
"city": "",
"state": "",
"zip": ""
}
}
映射:
+ (RKEntityMapping *)responseMapping {
if (_responseMapping == nil) {
_responseMapping = [RKEntityMapping mappingForEntityForName:[CMDUser entityName] inManagedObjectStore:[NSManagedObject managedObjectStore]];
_responseMapping.identificationAttributes = @[ CMDUserAttributes.userID, CMDUserAttributes.type ];
[_responseMapping addAttributeMappingsFromDictionary:@{
@"id" : CMDUserAttributes.userID,
@"type" : CMDUserAttributes.type,
@"profile_image_url" : CMDUserAttributes.profilePic,
@"credential_id" : CMDUserAttributes.credentialID,
@"name" : CMDUserAttributes.name,
@"last_name" : CMDUserAttributes.lastName,
@"first_name" : CMDUserAttributes.firstName,
@"allergies" : CMDUserAttributes.allergies,
@"medications" : CMDUserAttributes.medications,
@"history" : CMDUserAttributes.history,
@"email" : CMDUserAttributes.email,
@"location" : CMDUserAttributes.location,
@"zipcode" : CMDUserAttributes.zipcode,
@"gender" : CMDUserAttributes.gender,
@"dob" : CMDUserAttributes.dob,
@"relationship.state" : CMDUserAttributes.relationshipState,
@"relationship.status" : CMDUserAttributes.relationshipStatus,
@"relationship.onboarded_at" : CMDUserAttributes.onboardedAt,
@"auto_set_context_id" : CMDUserAttributes.autoSetContextID,
@"phone" : @"phone",
}];
}
// taking this line out removes the exception, but then the json isn't mapped
[_responseMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"assignments" toKeyPath:@"doctorAssignments" withMapping:[CMDDoctorAssignment mapping]]];
return _responseMapping;
}
启动时出现此错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to add mapping for keyPath doctorAssignments, one already exists...'
注意:我省略了json中的一些字段。在映射中,我没有
@"assignments": @"doctorAssignments"
。