例如。如果我有一个使用以下对象的聊天应用程序:ChatRoom
和 ChatMessage
。 (NSManagedObject
的两个子类)
在整个应用程序中,我需要:搜索聊天室/添加按摩/创建聊天室和消息/以及任何其他操作。
如果我直接用核心数据做所有事情可以吗?我的意思是每次我需要搜索聊天室或类似的东西时,都使用 NSFetchRequest 或 NSFetchedResultsController 来完成。
最佳答案
当然,您可以在需要时随时访问数据。另一方面,您应该尝试尽可能多地使用缓存机制。
例如,如果您在 UITableView
中使用数据,您一定应该使用 NSFetchedResultsController
,因为它是为 UITableViews
显式创建的。来自苹果docs在 NSFetchedResultsController
上:
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/