Foreign key relationship mapping with RestKit

ぐ巨炮叔叔 提交于 2019-11-26 06:07:45

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/17326087/foreign-key-relationship-mapping-with-restkit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!