我应该把 NSFetchedResultsControllerDelegate 放在哪里?
我正在自学编程,方法是使用 UINavigationController
制作一个简单的核心数据钻取应用程序,您可以在其中选择祖父母实体来查看父实体的 UITableView
,然后选择一位家长来看望孩子。每当用户选择一个项目时,我都会使用属性将 NSManagedObjectContext
和 NSFetchedResultsController
移交给下一个视图。每个视图控制器都是一个 UITableViewController,它们都遵循 NSFetchedResultsControllerDelegate 协议。
这工作正常,但意味着每个视图控制器都在实现委托方法等,这似乎效率低下。
为了使应用程序更简单,拥有一个被我的所有视图控制器引用的 NSFetchedResultsControllerDelegate
会更好吗?最好的地方是哪里——应用程序委托?
谢谢!
------------------------------------编辑-------------------- ---------
我正在尝试让 GorillaPatch 的答案在下面起作用。在我的子视图中,我有这个方法,它是模式“添加视图控制器”的委托方法:
- (void)addingViewController:(AddingViewController *)addingViewController didAdd:(NSString *)itemAdded
{
NSManagedObjectContext *context = [parent managedObjectContext];
Child *newChild = [NSEntityDescription insertNewObjectForEntityForName:@"Child" inManagedObjectContext:context];
[self.children insertObject:newChild atIndex:0];
newChild.name = itemAdded;
newChild.dateStamp = [NSDate date];
// Save the context.
NSError *error = nil;
if (![context save:&error])
{
// Handle The Error.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[self dismissModalViewControllerAnimated:YES];
}
并且头文件中有以下内容:
@property (nonatomic, retain) Trip *trip;
@property (nonatomic, retain) Checklist *checklist;
@property (nonatomic, retain) NSMutableArray *checklists;
I am teaching myself to program by making a simple core-data drill-down app with a UINavigationController
where you select a grandparent entity to see a UITableView
of parents, and then select a parent to see children. Whenever the user selects an item, I use properties to hand over the NSManagedObjectContext
and NSFetchedResultsController
to the next view. Each view controller is a UITableViewController
, and they all conform to the NSFetchedResultsControllerDelegate
Protocol.
This works fine, but means every view controller is implementing the delegate methods etc., which seems inefficient.
To make the app simpler, would it be better to have a single NSFetchedResultsControllerDelegate
that is referenced by all my view controllers? And where would the best place for this be - the app delegate?
Thanks!
---------------------------EDIT----------------------------
I'm trying to get GorillaPatch's answer to work below. In my child view, I have this method which is a delegate method for the modal "Adding View Controller":
- (void)addingViewController:(AddingViewController *)addingViewController didAdd:(NSString *)itemAdded
{
NSManagedObjectContext *context = [parent managedObjectContext];
Child *newChild = [NSEntityDescription insertNewObjectForEntityForName:@"Child" inManagedObjectContext:context];
[self.children insertObject:newChild atIndex:0];
newChild.name = itemAdded;
newChild.dateStamp = [NSDate date];
// Save the context.
NSError *error = nil;
if (![context save:&error])
{
// Handle The Error.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[self dismissModalViewControllerAnimated:YES];
}
And there is the following in the header file:
@property (nonatomic, retain) Trip *trip;
@property (nonatomic, retain) Checklist *checklist;
@property (nonatomic, retain) NSMutableArray *checklists;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
以我的拙见,绝对不是。一般来说,您需要为每个视图使用不同的 NSFetchedResultsControllerDelegate 实现,因为您在每个委托方法中实际执行的操作可能并且通常会有所不同(除非您的应用程序非常简单)。
In my humble opinion, definitely no. In general, you need a different
NSFetchedResultsControllerDelegate
implementation for each view, because what you actually do in each delegate method may and will usually differ (unless your application is really simple).我建议为每个 UITableView 使用 FetchedResultsController 实例。原因在于 FRC 响应数据更改的方式。您可以在此处阅读有关 FRC 的信息:http://developer.apple.com/library/ios/#documentation/CoreData/Reference/NSFetchedResultsController_Class/Reference/Reference.html
此外,如果您按照您所说的进行深入分析,每个新的子视图都应该是一组唯一的数据,不允许您共享 FRC。
I would recommend using a FetchedResultsController instance for each UITableView. The reason is because of the way that the FRC responds to changes of data.You can read about the FRC here: http://developer.apple.com/library/ios/#documentation/CoreData/Reference/NSFetchedResultsController_Class/Reference/Reference.html
Besides, if you're drilling down as you said, each new child view should be a unique set of data that would not allow you to share the FRC.
不,您可能会更好地编写一个实现 NSFetchedResultsControllerDelegate (可能是 UITableViewController 子类)并包含 NSFetchedResultsController 实例的基类,然后在需要的地方扩展您的基类。
如果您有多个级别要深入,最有可能的情况是您的实现中唯一会改变的是用于获取 NSFetchedResultsController 实例的谓词。
No, you'll probably be better off by writing a base class that implements NSFetchedResultsControllerDelegate (which would probably be a UITableViewController subclass) and contains a NSFetchedResultsController instance, and then extending your base class wherever you need it.
If you have multiple levels to drill down to, the most likely scenario is that the only thing that will change among your implementations is the predicate used to obtain your NSFetchedResultsController instance.
为什么要将
NSFetchedResultsController
和NSManagedObjectContext
(MOC) 交给子视图控制器或详细视图控制器?我强烈建议在详细视图控制器上定义一个属性,这是您想要显示的对象。例如,如果您有从 CoreData 获取的菜谱列表,并且您在菜谱上单击选项卡,则您将有一个滑动的详细视图控制器,其中将显示菜谱的详细信息。我建议通过具有 currentRecipe 实例变量的
UIViewController
子类来实现它。然后,您可以将此实例变量设置为您在列表中选择的配方,然后将视图控制器推送到堆栈上。通过这样做,您可以很好地解耦您的用户界面。这使得这个视图控制器可以在整个程序中重用。
更新
由于我们进行了冗长的讨论,我想提供更多的材料,如果您想了解更多有关 MVC 设计模式以及如何在 iPhone 上实现向下钻取导航的信息,这些材料可能会有所帮助。
Why do you hand over the
NSFetchedResultsController
and theNSManagedObjectContext
(MOC) to the child or detail view controller? I would strongly suggest defining a property on the detail view controller, which is the object you want to show.For example if you have a list of recipes fetched from CoreData and you tab on a recipe, you would have a detail view controller sliding in which would show the recipe's details. I would suggest implementing it by having a
UIViewController
subclass which has a currentRecipe instance variable. You would then set this instance variable to the recipe which you tabbed in your list and then push the view controller on the stack.By doing this you would decouple your user interface really nicely. This makes this view controller reusable in the whole program.
Update
Due to our lengthy discussion I would like to provide more material which could be helpful if you want to know more about MVC design patterns and how to implement a drill down navigation on the iPhone.
扩展之前的答案:
NSFetchedResultsController 是模型-视图-控制器应用程序设计的控制器层的一部分。设计的名称应该是模型-控制器-视图,因为控制器在数据模型(本例中为核心数据)和视图之间进行中介。因此,FRC 必须根据其获取、排序和管理数据的每个特定表视图的需求进行定制。它正确地属于表视图的数据源委托,通常只是表视图控制器对象。
您正在考虑的设计仅在每个表都使用具有完全相同的排序顺序的完全相同的实体时才有效。既然如此,为什么还要麻烦多个表呢?
To expand on the previous answers:
The NSFetchedResultsController is part of the controller layer of a Model-View-Controller app design. The name of the design should be Model-Controller-View because the controller mediates between the data model (Core Data in this case) and the view. As such the FRC has to be customized for the needs of each particular tableview whose data it fetches, sorts and manages. It properly belongs in the tableview's datasource delegate which is usually just the tableview controller object.
The design you are contemplating would only work if every single table used the exact same entity with the exact same sort order. In that case, why bother with multiple tables?