我应该在 iPhone 上预先获取所有核心数据实体吗?
我正在构建一个具有 chatView 的社交应用程序。消息是一个实体。在 usersViewController (rootViewController)上,我只想显示每个用户的未读消息数。但在 chatView 上,我想显示我和所选用户之间发送的消息。
我应该如何从 Core Data 获取消息?
有没有办法将它们全部提取到 NSMutableDictionary 中,以 userIds 作为键,每个都指向 Message 实体的 NSMutableArray?或者,我是否必须获取所有消息,然后使用 Objective-C 迭代它们以按描述的方式排列它们?
无论哪种方式,我应该将它们全部加载到 appDelegate 中吗?或者,我应该只在查看相应的 chatView 时加载特定对话的消息吗?
要获取未读计数,我是否应该迭代获取的消息实体以创建指向计数值的 userID 键的 NSMutableDictionary?或者,我应该创建一个类似于按 userId 分组的 SQL 求和查询的谓词(这对于 Core Data 来说是可能的吗?)?
我是否应该创建一个名为
UnreadMessageCount
的模型,用于存储每个用户的未读消息数以及对User
模型的引用?我希望将所有未读消息都放在应用程序的第一页上,这样查询速度会更快。但是接收消息会更慢,因为我不仅需要将其添加到 Message 模型中,而且还必须增加 UnreadMessageCount 模型中该用户的计数。此外,数据也会有重复。另外,每次应用程序启动时,我只会对所有消息计数进行一次查询,但每次收到新消息并且不在该用户的 chatView 页面上时,我都会进行查询以更新未读消息计数。
请告诉我有关 iPhone 内存管理和内存管理的信息。核心数据的可能性和性能。另外,请就设计此问题的最佳方法发表您的意见。
谢谢!
马特
I am building a social app that has a chatView. Message is an entity. On the usersViewController (rootViewController), I want to just display the number of unread messages from each user. But on the chatView, I want to show the messages sent between me and the selected user.
How should I fetch the messages from Core Data?
Is there a way to fetch them all into an NSMutableDictionary with userIds as keys, each pointing to an NSMutableArray of Message entities? Or, would I have to fetch all the Messages and then iterate through them with Objective-C to arrange them as described?
Either way, should I load them all upfront in the appDelegate? Or, should I only load the messages for the specific conversation upon viewing the corresponding chatView?
To get the unread counts, should I iterate through the fetched Message entities to make an NSMutableDictionary of userID keys pointing to count values? Or, should I make a predicate that acts like a SQL sum query grouped by userId (Is that even possible with Core Data?)?
Should I create a model, call it
UnreadMessageCount
, that stores the number of unread messages that I have from each user and a reference to theUser
model? I want to have all the unread messages on the first page of the app, so this would make that query faster. But receiving messages would be slower because I would not only have to add it to the Message model but also I would have to increment the count for that user in the UnreadMessageCount model. Also, the data would sort of be duplicated. Also, I'll only make the query for all message counts once per application launch, but i'll make queries to update the unread message count every time I receive a new message and am not on the chatView page with that user.
Please fill me in on iPhone memory management & Core Data possibilites and performance. And also please give your opinion on the best way to engineer this.
Thanks!
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用 CoreData 时,iPhone 通常可以很好地管理资源(尤其是示例中的简单结构)。因此,为了回答您的问题,我认为您应该这样做:
的
实体messages
属性的字典键userNSFetchedResultsController
来代替,它可以有效地加载所请求的实体,并执行所有所需的预取以在表视图中优化显示,iPhone generally does a great job managing resources when you use CoreData (especially simple structures as in your example). So to answer your questions, this is what I think you should do:
messages
property of each of theuser
entitiesNSFetchedResultsController
instead, it efficiently loads entities that are requested and does all the required prefetching for optimized display in table views要获取消息计数,我会向托管对象上下文询问给定特定获取请求的消息计数,例如,计算特定用户的消息数:
这里我假设
Message
实体具有用户
关系。要获取同一用户的实际消息,我只需将最后一行替换为:
To get the message count, I would ask the managed object context for the count of messages given a certain fetch request, for example, to count the messages for a certain user:
Here I'm assuming that the
Message
entity has auser
relationship.To get the actual messages for that same user, I would just replace the last line for: