RestKit:两个独立的提要,两种不同的对象类型。一个对象管理器?
我第一次使用 RestKit,非常高兴,不幸的是当前的项目让我陷入了困境。
首先,我有两个提要。一个用于新闻,一个用于照片...
新闻提要...
[
{"id":1234, "title":"Top story"},
{"id":1235, "title":"Next story"},
{"id":1236, "title":"Final story"}
]
照片提要...
[
{"id":1919, "url":"president.jpg"},
{"id":1920, "url":"celebrity.jpg"},
{"id":1921, "url":"sports.jpg"}
]
...在我的核心数据模型中,我创建了与这些属性匹配的这些实体。我还为 RestKit 设置了映射...
RKManagedObjectMapping* newsMapping = [RKManagedObjectMapping mappingForEntityWithName:@"News"];
newsMapping.primaryKeyAttribute = @"id";
[newsMapping mapKeyPath:@"id" toAttribute:@"id"];
[newsMapping mapKeyPath:@"title" toAttribute:@"article_id"];
[objectManager.mappingProvider setMapping:newsMapping forKeyPath:@"News"];
RKManagedObjectMapping* photoMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Photo"];
photoMapping.primaryKeyAttribute = @"id";
[photoMapping mapKeyPath:@"id" toAttribute:@"id"];
[photoMapping mapKeyPath:@"url" toAttribute:@"url"];
[objectManager.mappingProvider setMapping:photoMapping forKeyPath:@"Photo"];
...在上面的代码中,我知道提要中不存在指定的 keyPaths,但提供空的 keyPaths 似乎只是覆盖了映射,而 RestKit 似乎忽略了feed 中缺少关键路径。接下来,在每个显示对象的视图控制器中,我运行一些非常相似的代码...
RKObjectManager* objectManager = [RKObjectManager sharedManager];
// objectManager.inferMappingsFromObjectTypes = YES;
[objectManager loadObjectsAtResourcePath:@"json/news" delegate:self block:^(RKObjectLoader* loader) {
loader.objectMapping = [objectManager.mappingProvider objectMappingForClass:[News class]];
}];
...足够的背景。所有这些代码一起加载零新闻和零照片。如果我注释掉照片映射,我的所有新闻项目都会填充,如果我注释掉新闻映射,那么我的照片就会出现在商店中。
RestKit 无法将每个提要的内容与其映射匹配在一起。 setMapping:forKeyPath: 中指定的键路径将不起作用,因为提要没有指定任何内容。我想也许 objectManager.inferMappingsFromObjectTypes = YES 会让 RestKit 检查每个 feed 的内容并选择正确的映射,但事实并非如此。另外,我假设 objectMappingForClass: 会自动拾取正确的映射,但事实并非如此。有谁知道如何使这项工作有效?
更新:实际上,尽管 RestKit 报告应用程序第一次运行时没有加载任何项目,但如果我停止并再次运行,我现在会看到从缓存加载的所有 20 个新闻项目和 20 张照片。我通过运行一次来确认这一点,看到消息“将所有 0 个新闻对象缓存到线程本地存储”,然后立即打开 .sqlite 文件并按预期看到所有 20 个项目。
I'm working with RestKit for the first time and am pretty happy, unfortunately the current project has thrown me for a loop.
First, I have two feeds. One for news and one for photos...
News feed...
[
{"id":1234, "title":"Top story"},
{"id":1235, "title":"Next story"},
{"id":1236, "title":"Final story"}
]
Photos feed...
[
{"id":1919, "url":"president.jpg"},
{"id":1920, "url":"celebrity.jpg"},
{"id":1921, "url":"sports.jpg"}
]
...in my Core Data model I have these entities created that match these attributes. I have also setup my mappings for RestKit...
RKManagedObjectMapping* newsMapping = [RKManagedObjectMapping mappingForEntityWithName:@"News"];
newsMapping.primaryKeyAttribute = @"id";
[newsMapping mapKeyPath:@"id" toAttribute:@"id"];
[newsMapping mapKeyPath:@"title" toAttribute:@"article_id"];
[objectManager.mappingProvider setMapping:newsMapping forKeyPath:@"News"];
RKManagedObjectMapping* photoMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Photo"];
photoMapping.primaryKeyAttribute = @"id";
[photoMapping mapKeyPath:@"id" toAttribute:@"id"];
[photoMapping mapKeyPath:@"url" toAttribute:@"url"];
[objectManager.mappingProvider setMapping:photoMapping forKeyPath:@"Photo"];
...in the above code I'm aware that the specified keyPaths aren't present in the feeds, but providing empty keyPaths just seemed to overwrite the mappings and RestKit seemed to ignore the absent keyPAths in the feeds. Next, in each of the view controllers where I'm displaying the objects I run some pretty similar code...
RKObjectManager* objectManager = [RKObjectManager sharedManager];
// objectManager.inferMappingsFromObjectTypes = YES;
[objectManager loadObjectsAtResourcePath:@"json/news" delegate:self block:^(RKObjectLoader* loader) {
loader.objectMapping = [objectManager.mappingProvider objectMappingForClass:[News class]];
}];
...enough background. Together all this code loads zero news and zero photos. If I comment out the photos mappings, all of my news items populate and if I comment out the news mappings then my photos appear in the store.
RestKit can't match together each feed's content with its mapping. The keypaths specified in setMapping:forKeyPath: won't work because the feeds don't have anything specified. I thought maybe objectManager.inferMappingsFromObjectTypes = YES would have RestKit inspect what each feed had and choose the proper mappings, but it didn't. Also, I assumed objectMappingForClass: would automatically pickup the correct mappings, but no. Does anyone have any ideas how to make this work?
UPDATE: Actually, even though RestKit is reporting that no items had been loaded when the app runs the first time, if I stop and run again I now see all 20 news items and 20 photos loaded from cache. I confirmed this by running it once, seeing the message "Caching all 0 News objects to thread local storage" and then immediately opening the .sqlite file and seeing all 20 items as expected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此评论:https://groups.google.com/d/msg/ restkit/b0K0nCha8D0/VadnqDVB1-oJ 让我走上了正确的道路。我有一个带有现有核心数据堆栈的项目,一旦我删除了所有与其他存储集和上下文集相关的代码等。一切都很有魅力。多么棒的框架啊。
This comment: https://groups.google.com/d/msg/restkit/b0K0nCha8D0/VadnqDVB1-oJ sent me down the right path. I had a project with an existing Core Data stack and once I removed all related to code to that OTHER set of stores and contexts, etc. Everything worked a charm. What an awesome framework.