Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

cocoa - RestKit 2.0 : Mapping - second entitiy nil when not unique

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...