我应该在 iPhone 上预先获取所有核心数据实体吗?

发布于 2024-09-17 14:06:49 字数 959 浏览 7 评论 0原文

我正在构建一个具有 chatView 的社交应用程序。消息是一个实体。在 usersViewController (rootViewController)上,我只想显示每个用户的未读消息数。但在 chatView 上,我想显示我和所选用户之间发送的消息。

我应该如何从 Core Data 获取消息?

  1. 有没有办法将它们全部提取到 NSMutableDictionary 中,以 userIds 作为键,每个都指向 Message 实体的 NSMutableArray?或者,我是否必须获取所有消息,然后使用 Objective-C 迭代它们以按描述的方式排列它们?

  2. 无论哪种方式,我应该将它们全部加载到 appDelegate 中吗?或者,我应该只在查看相应的 chatView 时加载特定对话的消息吗?

  3. 要获取未读计数,我是否应该迭代获取的消息实体以创建指向计数值的 userID 键的 NSMutableDictionary?或者,我应该创建一个类似于按 userId 分组的 SQL 求和查询的谓词(这对于 Core Data 来说是可能的吗?)?

  4. 我是否应该创建一个名为 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?

  1. 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?

  2. 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?

  3. 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?)?

  4. 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 the User 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空‖城人不在 2024-09-24 14:08:11

当您使用 CoreData 时,iPhone 通常可以很好地管理资源(尤其是示例中的简单结构)。因此,为了回答您的问题,我认为您应该这样做:

  1. 您需要获取所有用户,然后迭代它们并创建指向每个 messages 属性的字典键user 实体
  2. 您不应该使用 NSFetchedResultsController 来代替,它可以有效地加载所请求的实体,并执行所有所需的预取以在表视图中优化显示,
  3. 上面的答案可能是正确的方法当谈到计数时。为您的实体创建适当的索引将使速度快如闪电。

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:

  1. You will need fetch all users, then iterate through them and create dictionary keys that will point at messages property of each of the user entities
  2. You shouldn't, use NSFetchedResultsController instead, it efficiently loads entities that are requested and does all the required prefetching for optimized display in table views
  3. the answer above is probably the way to go when it comes to counts. Creating proper indices on your entities will make it lightning fast.
猫卆 2024-09-24 14:07:52

要获取消息计数,我会向托管对象上下文询问给定特定获取请求的消息计数,例如,计算特定用户的消息数:

NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Message" inManagedObjectContext:context];
[request setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user == %@", user];
[request setPredicate:predicate];

NSInteger count = [context countForFetchRequest:request error:nil];

这里我假设 Message 实体具有用户关系。

要获取同一用户的实际消息,我只需将最后一行替换为:

NSArray *messages = [context executeFetchRequest:request error:nil];

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:

NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Message" inManagedObjectContext:context];
[request setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user == %@", user];
[request setPredicate:predicate];

NSInteger count = [context countForFetchRequest:request error:nil];

Here I'm assuming that the Message entity has a user relationship.

To get the actual messages for that same user, I would just replace the last line for:

NSArray *messages = [context executeFetchRequest:request error:nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文