0
votes

As a newbie RestKit user I'm having a conceptual understanding problem with CoreData relationship mapping.

Let's say we have a CoreData model with just two entities in master/detail relationship:

@interface Master : NSManagedObject
@property (nonatomic, retain) NSString *objectId;
@property (nonatomic, retain) NSString *display;
@property (nonatomic, retain) NSSet *childrens;
@end

@interface Children : NSManagedObject
@property (nonatomic, retain) NSString *objectId;
@property (nonatomic, retain) NSString *display;
@property (nonatomic, retain) Master *father;
@end

The RestKit mapping for this model is:

RKEntityMapping *masterMapping = ...
RKEntityMapping *childrenMapping = ...

... property mappings ...

masterMapping.identificationAttributes = @[ @"objectId" ];
childrenMapping.identificationAttributes = @[ @"objectId" ];

[masterMapping addPropertyMapping:[RKRelationshipMapping  
  relationshipMappingFromKeyPath:@"childrens" toKeyPath:@"childrens"
  withMapping:childrensMapping]];

[childrenMapping addPropertyMapping:[RKRelationshipMapping  
  relationshipMappingFromKeyPath:@"father" toKeyPath:@"father"
  withMapping:masterMapping]];

Here is how the http get response is modeled (I can change it):

{
 objectId: "3",
 display: "a master object",
 childrens: [
   {
     objectId: "1",
     display: "a child object",
     father: { objectId: "3" }
   },
   {
     objectId: "2",
     display: "another child object",
     father: { objectId: "3" }
   }
 ]

}

The problem is that the previous mapping definition would lead to a circular mapping error from RestKit when associating it with some RKResponseDescriptor.

I've read RestKit documentation and many stackoverflow.com similar threads, but still I don't understand how to set up a full CoreData model relationship mapping, provided that I need to have both relations available in my code (i.e. I need to explicitly access father from a children, and childrens from a master entity).

Any help will be appreciated.

Many thanks in advance!

1

1 Answers

0
votes

I've figured out by myself my mapping understanding problem, and I'd like to share that just in case: a RestKit model mapping is not meant to be defined on the model in "absolute" terms, but "relatively" to the RESTful operations on the model.

Put in other terms, a model entity can have more than one RestKit mapping, each corresponding to how that entity is referred in GET/POST/PATCH/etc. operations.

I hope this can help others to ease the understanding of RestKit beautiful framework!