Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

ios - Foreign key relationship mapping with RestKit

I'm totally new to RestKit and am struggling somewhat.

JSON:

{
    "teams": [
        {
            "id": 1,
            "name": "Team A"
        },
        {
            "id": 2,
            "name": "Team B"
        }
    ],
    "users": [
        {
            "id": 1,
            "name": "cameron",
            "teamId": 1
        },
        {
            "id": 2,
            "name": "paul",
            "teamId": 2
        }
    ]
}

CoreData:

@interface Team : NSManagedObject
@property (nonatomic, retain) NSNumber * teamId;
@property (nonatomic, retain) NSString * name;
@end




@interface User : NSManagedObject
@property (nonatomic, retain) NSNumber * userId;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) Team * team;
@end

My application logic looks like this:

// team mapping
RKEntityMapping *teamMapping = [RKEntityMapping mappingForEntityForName:@"Team" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
teamMapping.identificationAttributes = @[@"teamId"];
[teamMapping addAttributeMappingsFromDictionary:@{
 @"id": @"teamId",
 @"name": @"name"
 }];


// Register our mappings with the provider
RKResponseDescriptor *teamResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:teamMapping
                                                                                   pathPattern:nil
                                                                                       keyPath:@"teams"
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[self.objectManager addResponseDescriptor:teamResponseDescriptor];



// user mapping
RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
userMapping.identificationAttributes = @[@"userId"];
[userMapping addAttributeMappingsFromDictionary:@{
 @"id": @"userId",
 @"name": @"name"
 }];


// Register our mappings with the provider
RKResponseDescriptor *userResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:userMapping
                                                                                   pathPattern:nil
                                                                                       keyPath:@"users"
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[self.objectManager addResponseDescriptor:userResponseDescriptor];

I can't for the life of me work out how to get RestKit to populate the team Property of the user objects.

I've look at so many posts but nothing I try works, is this not a usual use case?

Does anyone know how to do this, help would be very appreciated!

Thanks.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You need to add a transient attribute to your User entity which holds the associated teamId. This needs to be added to your userMapping.

Then, you need to add a relationship definition to your userMapping:

[userMapping addConnectionForRelationship:@"team" connectedBy:@{ @"teamId": @"teamId" }];

This gives RestKit the information it needs to make the connection and instructs it to make the connection as part of the mapping operation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...