问题
I am trying to map an array of 'Comments' to a Comment entity that has a sub-entity 'testUser', the person who wrote the comment. Everything is mapping fine as long as the testUsers for all comments are unique.
If a user writes two comments, the second comment will have a nil testUser object. RestKit is mapping the second comment's testUser to the same object as the first testUser, even they belong to two seperate comment-objects. You can see this in the mapping results below. How can I avoid this happening?
Many many thanks. Al
// Test one mapping result
Mapped relationship object from keyPath 'user' to 'testUser'. Value: (entity: TestUser; id: 0x17e1a070 ; data: {
comment = "0x17e4d9b0 <x-coredata:///Comment/tC0FCC2A0-6B2A-4130-A711-6E4A983D40F258>";
testFirstName = Sam;
testID = 183599;
testLastName = Doe;
})
// Test two mapping result
Mapped relationship object from keyPath 'user' to 'testUser'. Value: (entity: TestUser; id: 0x17e1a070 ; data: {
comment = "0x15e72aa0 <x-coredata://5038C2F2-93CF-46BF-BB2A-66F2423FA2DE/Comment/p46>";
testFirstName = Sam;
testID = 183599;
testLastName = Doe;
})
// Comments Mapping result
2013-11-23 11:45:42.689 app[4619:8333] T restkit.object_mapping:RKMappingOperation.m:722 Mapped NSSet relationship object from keyPath 'items' to 'items'. Value:
{(
(entity: Comment; id: 0x15eb9660 ; data: {
commentId = 407381;
createdAt = "2013-11-23 10:45:13 +0000";
testUser = "0x17e1a070 <x-coredata://5038C2F2-93CF-46BF-BB2A-66F2423FA2DE/TestUser/p9>";
text = "Test one";
}), (entity: Comment; id: 0x17e4d9b0 ; data: {
commentId = 407382;
createdAt = "2013-11-23 10:45:30 +0000";
testUser = nil;
text = "Test two";
}) )}
// Entities
Commments (with relationship to)-> Comment (with relationship t0)-> TestUser
回答1:
You haven't shown mappings, and I'm assuming that you have a to-many relationship to support what you're trying to do.
When you create the relationship mapping you need to set the assignmentPolicy property to RKUnionAssignmentPolicy. The default policy replaces any existing connection with the new one. This policy will merge the connections instead.
来源:https://stackoverflow.com/questions/20162007/restkit-2-0-mapping-second-entitiy-nil-when-not-unique