考虑以下 NSManagedObjects
1) 任务
@interface ITask : NSManagedObject
@property (nonatomic, retain) NSString * serialNumber;
@property (nonatomic, retain) NSSet *taskSharedWith;
@end
代表为
<entity name="ITask" representedClassName="ITask" syncable="YES">
<attribute name="serialNumber" optional="YES" attributeType="String" syncable="YES"/>
<relationship name="taskSharedWith" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="ITaskSharedWith" inverseName="task" inverseEntity="ITaskSharedWith" syncable="YES"/>
</entity>
<entity name="ITaskSharedWith" representedClassName="ITaskSharedWith" syncable="YES">
<relationship name="task" optional="YES" minCount="1" maxCount="1" deletionRule="Nullify" destinationEntity="ITask" inverseName="taskSharedWith" inverseEntity="ITask" syncable="YES"/>
<relationship name="user" optional="YES" minCount="1" maxCount="1" deletionRule="Nullify" destinationEntity="IUser" inverseName="taskSharedWith" inverseEntity="IUser" syncable="YES"/>
</entity>
<entity name="IUser" representedClassName="IUser" syncable="YES">
<attribute name="userDisplayName" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="userEmail" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="userFqn" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="userManager" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="userName" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="userProperties" optional="YES" attributeType="String" syncable="YES"/>
<relationship name="taskComments" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="ITaskComments" inverseName="user" inverseEntity="ITaskComments" syncable="YES"/>
<relationship name="taskSharedWith" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="ITaskSharedWith" inverseName="user" inverseEntity="ITaskSharedWith" syncable="YES"/>
<relationship name="userVideoRecordings" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="ITaskVideos" inverseName="user" inverseEntity="ITaskVideos" syncable="YES"/>
</entity>
2) 用户
@interface IUser : NSManagedObject
@property (nonatomic, retain) NSString * userDisplayName;
@property (nonatomic, retain) NSString * userEmail;
@property (nonatomic, retain) NSString * userFqn;
@property (nonatomic, retain) NSString * userID;
@property (nonatomic, retain) NSString * userName;
@property (nonatomic, retain) NSString * userProperties;
@end
这是传入的 JSON
[
{
"SerialNumber": "3_9",
"DelegatedUsers": [
{
"Name": "K2:DENALLIX\\k2service"
}
]
}
]
这是我当前的 ITask 映射:
RKEntityMapping *taskMapping = [RKEntityMapping mappingForEntityForName:@"ITask" inManagedObjectStore:mos];
//primary key
taskMapping.identificationAttributes = @[@"serialNumber"];
[taskMapping addAttributeMappingsFromDictionary:@{
@"SerialNumber":@"serialNumber",
}];
RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"IUser" inManagedObjectStore:mos];
userMapping.identificationAttributes = @[@"userFqn"];
[userMapping addAttributeMappingsFromDictionary:@{
@"Name":@"userFqn",
}];
RKEntityMapping *sharedWithMapping = [RKEntityMapping mappingForEntityForName:@"ITaskSharedWith" inManagedObjectStore:mos];
[sharedWithMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"DelegatedUsers"
toKeyPath:@"user"
withMapping:userMapping]];
[taskMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"DelegatedUsers"
toKeyPath:@"taskSharedWith"
withMapping:sharedWithMapping]];
Restkit 给了我这个错误
2013-05-29 00:15:48.647 k2Mobile[22389:c07] D restkit.object_mapping:RKMappingOperation.m:651 Mapping one to many relationship value at keyPath 'DelegatedUsers' to 'taskSharedWith'
2013-05-29 00:15:48.648 k2Mobile[22389:c07] D restkit.object_mapping:RKMappingOperation.m:862 Starting mapping operation...
Unknown.m:0: error: -[TaskMappingsFixture test_That_Task_Fetch_Mapping_is_Correct] : [<__NSCFConstantString 0x97d8fa0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key DelegatedUsers.
数据库中已经填充了用户,所以我只需要将条目插入到 ITaskSharedWith 表中,该表与 ITask 和 IUser 具有多对多关系