- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在努力将我的 JSON API 返回的外键关系映射到 RestKit。
具体来说,我使用 Loopback 生成一个 API,其中包含 Team
和 User
等实体。默认情况下有两个端点返回以下 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 routes 、 Foreign key relationship mapping with RestKit 、 Restkit: 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
属性建立关系。该映射适用于包含 Team
和 User
对象作为数组的 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/
我使用 RestKit 与 REST api 进行交互。对于某些操作,例如 HTTP PUT/POST/DELETE,我只关心响应状态代码(200 或 500 等),而不关心响应数据,即使 API 确
我在客户端和服务器端都有一个这样的响应对象: 客户端: //BaseResponse.h @interface BaseResponse : NSObject{ NSString *code;
我正在尝试将 RestKit 0.20 集成到我的项目中,例如,我使用 google rss 阅读器作为示例项目。 这是对 Xml 的响应 / /类别> & 这是我必须从中获取数据的 Xml。 RKO
我很难使用 CocoaPods 将 RestKit 安装到我的项目中。我收到未找到的错误。 我的 Pods.xcconfig 看起来像这样: GCC_PREPROCESSOR_DEFINITIONS
大多数 RESTful API 都会路由对资源的访问,例如 Rabbit 对象,如下所示: GET /rabbits <--- GET all rabbits.
问题 在 iOS 上完成 Restkit 的安装说明后,我收到 Apple Mach-o 链接器错误: 我的流程 我尝试按照此处的 Restkit 安装说明进行操作:https://github.co
我完全按照此处找到的这些步骤操作: http://www.raywenderlich.com/13097/intro-to-restkit-tutorial 但是....我得到了错误: 这可能是什么原
我正在尝试使用 RestKit 调用需要基本身份验证的端点。 RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[JSNCateg
我想用RestKit ,但我已经使用 Realm.io而不是 CoreData。 是否可以在 Realm.io 之上使用 RestKit? 最佳答案 你当然可以。从 RestKit 取回对象后: //
我没有找到任何方法来设置 restkit 0.20.0 的超时间隔。 任何人都可以帮助增加超时间隔。 谢谢 最佳答案 子类 RKHTTPRequestOperation 和实现方法 - (NSURLR
我正在尝试实现 RESTKit 0.20 操作队列,我在博客上读到 NSOperationQueue 也可用于创建操作队列。我想使用 RestKit 操作队列的 native 方法。 任何人都可以发布
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 8年前关闭。 Improve thi
我在这里做的事情很奇怪吗? 我的类别得到下载和映射, 产品也像日志记录所说的那样被下载和映射, 但是我的产品在每个分类下都是空的,谢谢! { "productsCategories" : [
我尝试了几乎所有的解决方案,但都不适合我。我在 Xcode 6 上收到此错误。我也更新了 cocoa pod ,也更新了 pod 文件。 安装更新后,我在下面添加了几行: [!] From now o
我正在尝试使用 Jenkins-CI 自动构建一个使用 Restkit 的 iOS 项目,显然,restkit 作为一个项目驻留在主项目中。该项目使用 XCode-IDE 成功构建。 但是当我使用 x
我在 Xcode 6.1 中开发项目。它工作正常,突然提示错误“未找到 Restkit/Restkit.h 文件”。从 $ 更改Architectures 后出现错误(ARCHS_STANDARD_3
我按照 github 上的说明进行操作页面但有问题(肯定是我:P)但是,您知道在 Xcode4 RestKit 中集成/使用的任何好的教程吗? 谢谢! 最佳答案 从此链接下载最新版本 http://r
我在尝试让 RestKit 构建和工作时遇到了麻烦。我收到可怕的“词法或预处理器问题:未找到 RestKit/RestKit.h 文件”消息。我使用 Xcode 4.2,我的项目适用于 iOS。 早些
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:BASE_URL]]; PPPS
有没有办法使用对象管理器访问请求的成功块中的响应数据? [objectManager postObject:[User class] path:@"/users" parameters:nil suc
我是一名优秀的程序员,十分优秀!