问题描述
我为没有NSFetchedResultsController实例的iPhone应用程序设置了Core Data。
I have setup Core Data for an iPhone app without an instance of NSFetchedResultsController.
为此,我创建了一个创建的模型类来封装所有核心数据请求和构建NSFetchRequests / NSPredicates。
To do this, I created a created a model class to encapsulate all core data requests and constructing of NSFetchRequests/NSPredicates.
这保留了我的UITableViewController中的所有Core Data特定代码。
This kept all Core Data specific code out of my UITableViewController.
现在我想添加NSFetchedResultsController以便更容易填充分段的UITableView。
Now I want to add NSFetchedResultsController to make populating a sectioned UITableView easier.
我的问题是:在所有示例中,我看到实例NSFetchedResultsController是UITableViewController的ivar。这导致在UITableViewController中构造所有NSFetchedResults语句。
My issue is this: In all examples I have seen the instance NSFetchedResultsController is an ivar of the UITableViewController. This leads to all NSFetchedResults statements constructed within the UITableViewController.
我觉得这会使UITableViewController变得混乱,并增加了另一个责任。
I feel like this is clutters the UITableViewController and adds another responsibility.
你怎么处理这个?
使用NSFetchedResultsController时,对象图形是什么样的?
What does your object graph look like when using NSFetchedResultsController?
你在哪里构建你的NSPredicates?
Where do you construct your NSPredicates?
推荐答案
更新:
I已经回到在UITableViewControllers中构建NSFetchedResultsControllers。
I've gone back to constructing NSFetchedResultsControllers within the UITableViewControllers.
我也一直在NSManagedObjectContext上使用类别进行查询。另外,我在NSmanagedObject子类的类别中添加了更多的逻辑。
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.
原始答案:
我想我我将发表自己对此事的看法。
I guess I'll post my own take on the matter.
由于NSFetchedResultsController是一个Foundation类而不是UIKit类,因此它向我发出了模型。
Since NSFetchedResultsController is a Foundation class and not a UIKit class, it screams model to me.
同样适用于NSFetchRequest。
Same thing for NSFetchRequest.
似乎构建一个NSFetchRequest依赖于数据模型的知识,你可以认为UITableViewController应该是我不知道。
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.
一个选项是子类NSFetchedResultsController,make就像一个工厂类,我返回完全配置的NSFetchedResultsController实例。这保留了NSFetchRequest构造。
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.
另一个选项非常相似,但是我创建了一个处理相同工作的NSObject子类或类别,而不是子类化NSFetchedResultsController。
Another option is very similar, but instead of subclassing NSFetchedResultsController, I create a NSObject subclass or category that handles the same work.
这两个选项似乎都比使用UITableViewController执行这些任务更符合MVC。
Both of these options seem to conform to MVC better than using the UITableViewController to perform those tasks.
这篇关于NSFetchedResultsController和构造NSFetchRequests的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!