我应该把 NSFetchedResultsControllerDelegate 放在哪里?

发布于 2024-10-30 06:55:50 字数 1567 浏览 1 评论 0原文

我正在自学编程,方法是使用 UINavigationController 制作一个简单的核心数据钻取应用程序,您可以在其中选择祖父母实体来查看父实体的 UITableView,然后选择一位家长来看望孩子。每当用户选择一个项目时,我都会使用属性将 NSManagedObjectContextNSFetchedResultsController 移交给下一个视图。每个视图控制器都是一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情深如许 2024-11-06 06:55:51

以我的拙见,绝对不是。一般来说,您需要为每个视图使用不同的 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).

骑趴 2024-11-06 06:55:51

我建议为每个 UITableView 使用 FetchedResultsController 实例。原因在于 FRC 响应数据更改的方式。您可以在此处阅读有关 FRC 的信息:http://developer.apple.com/library/ios/#documentation/CoreData/Reference/NSFetchedResultsController_Class/Reference/Reference.html

如果您为获取的数据设置委托
结果控制器,控制器
注册以接收更改
来自其管理对象的通知
语境。上下文的任何变化
影响结果集或部分
信息被处理并且
结果会相应更新。这
控制器通知代表何时
结果对象改变位置或何时
部分已修改(参见
NSFetchedResultsControllerDelegate)。
您通常使用这些方法来
更新表格视图的显示。

此外,如果您按照您所说的进行深入分析,每个新的子视图都应该是一组唯一的数据,不允许您共享 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

If you set a delegate for a fetched
results controller, the controller
registers to receive change
notifications from its managed object
context. Any change in the context
that affects the result set or section
information is processed and the
results are updated accordingly. The
controller notifies the delegate when
result objects change location or when
sections are modified (see
NSFetchedResultsControllerDelegate).
You typically use these methods to
update the display of the table view.

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.

萌酱 2024-11-06 06:55:51

不,您可能会更好地编写一个实现 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.

萌酱 2024-11-06 06:55:51

为什么要将 NSFetchedResultsControllerNSManagedObjectContext (MOC) 交给子视图控制器或详细视图控制器?我强烈建议在详细视图控制器上定义一个属性,这是您想要显示的对象。

例如,如果您有从 CoreData 获取的菜谱列表,并且您在菜谱上单击选项卡,则您将有一个滑动的详细视图控制器,其中将显示菜谱的详细信息。我建议通过具有 currentRecipe 实例变量的 UIViewController 子类来实现它。然后,您可以将此实例变量设置为您在列表中选择的配方,然后将视图控制器推送到堆栈上。

通过这样做,您可以很好地解耦您的用户界面。这使得这个视图控制器可以在整个程序中重用。

更新

由于我们进行了冗长的讨论,我想提供更多的材料,如果您想了解更多有关 MVC 设计模式以及如何在 iPhone 上实现向下钻取导航的信息,这些材料可能会有所帮助。

  1. 示例代码:查看iPhoneCoreDataRecipes 应用程序,该应用程序在 WWDC09 和 WWDC10 上进行了演示,以说明如何实现细节视图控制器堆栈以及它们如何相互交互。
  2. WWDC 会议视频:有一些 WWDC 会议视频可能会有所帮助:
    • WWDC10:第 116 场会议 - iPhone 操作系统的模型-视图-控制器
    • WWDC09:第 125 场会议 - 有效的 iPhone 应用架构

Why do you hand over the NSFetchedResultsController and the NSManagedObjectContext (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.

  1. Sample code: Have a look at the iPhoneCoreDataRecipes app which was demoed on WWDC09 and WWDC10 to illustrate how to implement a stack of detail view controllers and how they interact with each other.
  2. WWDC session videos: there are some WWDC session videos which could be helpful:
    • WWDC10: Session 116 - Model-View-Controller for iPhone OS
    • WWDC09: Session 125 - Effective iPhone App Architecture
最冷一天 2024-11-06 06:55:51

扩展之前的答案:

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文