gpt4 book ai didi

ios - RestKit:为外键关系创建 stub

转载 作者:可可西里 更新时间:2023-11-01 05:53:53 26 4
gpt4 key购买 nike

我正在努力将我的 JSON API 返回的外键关系映射到 RestKit。

具体来说,我使用 Loopback 生成一个 API,其中包含 TeamUser 等实体。默认情况下有两个端点返回以下 JSON:

/团队/

[
{
"title": "Team Title",
"id": 1,
}
]

/用户/

[
{
"name": "Alice",
"id": 1,
"teamId": 1,
},
{
"name": "Bob",
"id": 2,
"teamId": 1,
}, ...
]

现在这是一个非常简单的 API,对吧?使用 RestKit 进行映射应该很容易,但即使在阅读了有关该主题的所有其他问题(例如 Seeking recommendations for best RestKit/CoreData mapping and JSON structure for shallow routesForeign key relationship mapping with RestKitRestkit: GET remote linked object when foreign key refers to missing local object in Core Data )之后,我还是无法弄清楚如何将整个对象图加载到核心数据中。

如果它能让事情变得更简单,我当然也可以更改 API。出于显而易见的原因,我不想在每个 User 中嵌入整个 Team 对象,而是使用外键。另外,我想将 Endpoints 分开,主要是因为这是 Loopback 默认生成 API 的方式。

所以我尝试使用以下映射调用两个端点(顺序无关紧要):

团队映射

RKEntityMapping *teamMapping = [RKEntityMapping mappingForEntityForName:@"Team" inManagedObjectStore:objectManager.managedObjectStore];
[teamMapping addAttributeMappingsFromArray:@[ @"title", @"id" ]];
teamMapping.identificationAttributes = @[ @"id" ];
[objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:teamMapping method:RKRequestMethodAny pathPattern:@"Teams" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

用户映射

RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:objectManager.managedObjectStore];
[userMapping addAttributeMappingsFromArray:@[ @"name", @"id", @"teamId" ]];
userMapping.identificationAttributes = @[ @"id" ];
[userMapping addConnectionForRelationship:@"team" connectedBy:@{ @"teamId": @"id" }];
[objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:userMapping method:RKRequestMethodGET pathPattern:@"Users" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

正如其他问题(如上面链接的问题)中所建议的,我使用 RKConnectionDescription 通过 transient teamId 属性建立关系。该映射适用于包含 TeamUser 对象作为数组的 JSON 有效负载(在响应描述符中使用 keyPath 而不是 pathPattern),但会因单独的端点而失败我彼此独立地称呼。在那种情况下,连接关系所指的对象可能还没有被创建。因此,为了解决这个问题,应该创建一个 Stub 对象,在这种情况下只填充 id 属性。

如何使用 RestKit 映射实现这一点?


编辑 如果我将 JSON 结构从 "teamId": 1 更改为 "team": { "id": 1 } 会有帮助吗 并使用 keyPath team 添加另一个响应描述符?不确定使用 Loopback 是否容易做到这一点。执行此操作的最佳方法是什么?


编辑 2: 所以我添加了以下 stub 映射:

RKEntityMapping *teamStubMapping = [RKEntityMapping mappingForEntityForName:@"Team inManagedObjectStore:objectManager.managedObjectStore];
[teamStubMapping addAttributeMappingsFromDictionary:@{@"teamId": @"id"}];
teamStubMapping.identificationAttributes = @[ @"id" ];
[objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:teamStubMapping method:RKRequestMethodAny pathPattern:@"Users" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

但是当我打开跟踪记录时,我没有看到这个映射被执行。我究竟做错了什么?这个映射是否因为已经有另一个匹配相同的模式而被跳过?当我注释掉 User 映射时,它就完成了它的工作。


编辑 3: 所以 RestKit 确实实际上只使用添加到那些匹配给定路径和相同键路径的对象管理器的最后一个响应描述符,如讨论的 here on GitHub .感谢下面的答案,使用 nil 键路径映射解决了这个问题:

RKEntityMapping *teamStubMapping = [RKEntityMapping mappingForEntityForName:@"Team inManagedObjectStore:objectManager.managedObjectStore];
[teamStubMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"id"]];
teamStubMapping.identificationAttributes = @[ @"id" ];
[objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:teamStubMapping method:RKRequestMethodAny pathPattern:@"Users" keyPath:@"teamId" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

最佳答案

我认为我的以下答案以前有效,但也许我总是在关于响应描述符的精确匹配的评论中偷偷绕过这个问题。

因此,创建另一个映射,但将其设为 nil keypath mapping并在 @"teamId" 的响应描述符上使用键路径。这将导致 RestKit 从响应中提取一组团队 ID,并将它们作为对象项处理,与用户对象分开。


JSON 没问题。只需创建一个使用 teamStubMapping 的新响应描述符,这是一个类似于 teamMapping 的新映射,但将 id 替换为 teamId。此响应描述符的路径模式应为 @"Users"

关于ios - RestKit:为外键关系创建 stub ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24701657/

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