从关系数据快速创建核心数据关系 (iOS)
我正在使用 Core Data 来存储以“关系”形式发送的服务器数据。
例如,我从服务器获得的数据看起来像这样(实际格式有些不同,但足够相似)
Users: [{PK: 1, Name: 'A B'}, {PK: 2, Name: 'C D'}, {PK: 3, Name: 'E F'}]
Posts: [{PK: 1, UserPK: 1, Content: '...'}, {PK: 2, UserPK: 3, Content: '...'}]
我的核心数据模型在用户和帖子之间设置了关系,并且它的工作方式如下。然而,我的问题是当我从服务器收到新的数据集时,尽可能快地创建这些对象及其关系(不使用大量的 RAM)。
问题在于将帖子与用户关联起来。 “正常”的方法基本上是编写 post.user = user;
但是,这要求我从磁盘加载 user
。
我当前的计划是加载所有用户,对它们进行故障处理并创建一个将 PK 映射到实际对象的 NSDictionary。这样我就可以快速找到我需要与该帖子关联的用户。然而,对于本质上应该是相当简单的操作的事情来说,这个解决方案似乎有点复杂。
I am using Core Data to store data from the server that is sent in a "relational" form.
For example, the data I get from the server looks something like this (the actual format is somewhat different, but similar enough)
Users: [{PK: 1, Name: 'A B'}, {PK: 2, Name: 'C D'}, {PK: 3, Name: 'E F'}]
Posts: [{PK: 1, UserPK: 1, Content: '...'}, {PK: 2, UserPK: 3, Content: '...'}]
My Core Data model has an relationship set between Users and Posts and it works like it should. However, my problem is creating these objects and their relationships as fast as possible (without using ridiculous amounts of RAM) when I receive a new dataset from the server.
The problem is associating the posts with the users. The "normal" way to do it is to basically write post.user = user;
However, this requires that I have the user
loaded from the disk.
My current plan is to load all users, fault them and create a NSDictionary that maps the PK's to the actual objects. That way I can quickly find out the user I need to associate with the post. However, this solution seems a bit complex for something that should essentially be a rather trivial operation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将用户保存在
NSDictionary
内存中的解决方案是合理的。只要您将它们置于错误状态,它们就不应该占用太多内存。有缺陷的 MO很小。Your solution of keeping the users in memory in a
NSDictionary
is sound. They should not take very much memory at all as long as you leave them in a faulted state. A faulted MO is tiny.我遇到过非常类似的情况,最终根本没有将数据导入到核心数据中,因为数百个数据对象和关系的重复导入速度太慢了。事实证明,我为该格式编写的解析器足够快,可以实时读取数据(同时滚动表格视图等),所以我基本上只是将原始数据保存到磁盘。
解析器需要扩展,它现在输出类似于托管对象的数据对象,这允许使用 KVC 查询原始数据。
我继续使用 Core Data 来保存用户编辑,并引入了一个数据管理器类来将两个数据源整合在一起。
我想,不完全是你所要求的,但也许它有帮助......
I had a very similar situation and ended up not importing the data into core data at all, because the repeated imports for hundreds of data objects plus relationships where just too slow. It turned out that the parser I wrote for that format was sufficiently fast to read data in realtime (while scrolling tableviews etc.) though, so I basically just saved the raw data to disk.
The parser needed to be expanded, it now outputs data objects similar to managed objects, which allows to query the raw data using KVC.
I kept using Core Data for saving user edits and introduced a data manager class to bring both data sources together.
Not quite what you asked for, I guess, but maybe it helps...