我是RestKit的新手,正在挣扎。
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 } ] }
核心数据:
@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
我的应用程序逻辑如下所示:
// 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];
我一生都无法解决如何使RestKit填充用户对象的团队Property的问题。
我看过这么多帖子,但是我没有尝试,这不是通常的用例吗?
有谁知道该怎么做,帮助将不胜感激!
谢谢。
您需要向User持有关联的实体添加一个临时属性teamId。这需要添加到您的中userMapping。
User
teamId
userMapping
然后,您需要为您添加一个关系定义userMapping:
[userMapping addConnectionForRelationship:@"team" connectedBy:@{ @"teamId": @"teamId" }];
这将为RestKit提供建立连接所需的信息,并指示其进行映射操作的一部分以进行连接。