objective-c - 每当我需要操作业务对象时,直接使用核心数据是一种好的做法吗?

标签 objective-c ios database cocoa-touch core-data

例如。如果我有一个使用以下对象的聊天应用程序:ChatRoomChatMessage。 (NSManagedObject 的两个子类)
在整个应用程序中,我需要:搜索聊天室/添加按摩/创建聊天室和消息/以及任何其他操作。
如果我直接用核心数据做所有事情可以吗?我的意思是每次我需要搜索聊天室或类似的东西时,都使用 NSFetchRequest 或 NSFetchedResultsController 来完成。

最佳答案

当然,您可以在需要时随时访问数据。另一方面,您应该尝试尽可能多地使用缓存机制。

例如,如果您在 UITableView 中使用数据,您一定应该使用 NSFetchedResultsController,因为它是为 UITableViews 显式创建的。来自苹果docsNSFetchedResultsController 上:

It optionally monitors changes to objects in its associated managed object context, and reports changes in the results set to its delegate (see “The Controller’s Delegate”). It optionally caches the results of its computation so that if the same data is subsequently re-displayed, the work does not have to be repeated (see “The Cache”).

否则,如果您只是暂时需要数据,当然可以在每次需要时使用 NSFetchRequest 访问它们,或者如果它们没有更改或者您知道它们的情况,则将它们缓存在业务对象中否则终身。

关于objective-c - 每当我需要操作业务对象时,直接使用核心数据是一种好的做法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10356025/

相关文章:

ios - 使用 faSTLane 上传到 iTunes connect 时出错

objective-c - 如何像 "filteredArrayUsingPredicate"那样过滤NSFetchedResultsController

mysql - 触发更新 mysql 表中的值

python - 允许重复键的 python 中的 BTree 实现?

objective-c - 帮助发送/接收 UDP 数据包 - C 套接字

ios - 并行运行 ios 的 appium 脚本

ios - UITableViewCell 的子类不显示文本

mysql - 如果帖子标题包含文本,则批量向 WordPress 帖子添加分类法

ios - If 语句未运行 - iOS

ios - 如何将我的设备 token (NSData) 转换为 NSString?