gpt4 book ai didi

ios - 与 RestKit 的外键关系映射

转载 作者:技术小花猫 更新时间:2023-10-29 10:46:45 24 4
gpt4 key购买 nike

我是 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 填充用户对象的团队属性。

我看了很多帖子,但我尝试的都不起作用,这不是一个常见的用例吗?

有谁知道如何做到这一点,非常感谢帮助!

谢谢。

最佳答案

您需要向包含关联的 teamIdUser 实体添加一个 transient 属性。这需要添加到您的 userMapping

然后,您需要将关系定义添加到您的userMapping:

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

这为 RestKit 提供了建立连接所需的信息,并指示它建立连接作为映射操作的一部分。

关于ios - 与 RestKit 的外键关系映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17326087/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com