NSFetchedResultsController and constructing NSFetchRequests

这一生的挚爱 提交于 2019-12-04 16:59:59

I have a bunch of methods in my model class that returns various kinds of predicates, but I construct the actual NSFetchRequest and NSFetchedResultsController in the UITableViewController subclass. The reason I do it that way is because the user can set options about what criteria he wants to include, so that's properly a view-controller-related item, and the view controller needs easy access to the NSFetchedResultsController.

If the user doesn't have any say in the matter, I'd just have the UITableViewController get a NSFetchedResultsController when it loads.

Update:

I've gone back to constructing NSFetchedResultsControllers within the UITableViewControllers.

I've also been using a category on NSManagedObjectContext for queries. Additionally, I have been putting more logic within categories on my NSmanagedObject subclasses.

Overall, I am happy with the architecture I have been using as of late.


Original Answer:

I guess I'll post my own take on the matter.

Since NSFetchedResultsController is a Foundation class and not a UIKit class, it screams model to me.

Same thing for NSFetchRequest.

It seems that constructing a an NSFetchRequest relies on knowledge of the data model, which you could argue a UITableViewController should have no knowledge of.

This leading me to several ideas about what the object graph should look like.

One option is to subclass NSFetchedResultsController and make is like a factory class, where I return fully configured instances of NSFetchedResultsController. This keeps NSFetchRequest Construction.

Another option is very similar, but instead of subclassing NSFetchedResultsController, I create a NSObject subclass or category that handles the same work.

Both of these options seem to conform to MVC better than using the UITableViewController to perform those tasks.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!