2010-11-07 76 views
0

RootViewController我对核心数据存储(?)执行一个提取请求,它返回一个NSSet,比方说,食谱。当用户在rootviewcontroller中选择配方时,我执行pushViewController并将其中一个配方推送到childcontroller。NSFetchedResultsController和childController

在childController中,用户可以刷新“配方”,以便他可以更新配料。当用户选择“刷新”时,我连接到interwebs以接收新数据。这个新数据我puth在self.recipe.ingredients。

这一切都很好。但后来我想重新加载chilController视图。所有刷新的东西都是在后台完成的,所以我想在核心数据内容完成时发出一个信号。

Hello NSFetchedResultsController!但要使用NSFetchedResultsController,你需要一个NSFetchRequest。而且NSFetchRequest在我的rootViewController中实例化。

因此,当我在我的rootViewController中创建一个controllerDidChangeContent方法时,我发现它被触发。但现在是一个问题:我如何更新我的childViewController的tableview?

我想到了打电话我rootViewControllers' controllerDidChangeContent方法,这个方法: [self.childController controllerDidChangeContent:控制器] 所以刚好路过的烧制方法

或者实例化childController的时候,我也可以通过对fetchRequest到childController并在childController实例中实例化一个新的NSFetchedResultsController。

这两个最好的是什么?或者我还没有想过其他可能性?

提前致谢!

回答

0

我在我的一个项目中解决了这个问题。我有孩子控制器创建并使用自己的NSFetchRequest。它运作良好,但我很想知道是否有人推荐一种不同的方法。

因此,使用你的应用程序的条款,在这里它:在childController中,我使用Ingredient实体创建了一个全新的NSFetchRequest,并在其上放置了一个谓词以从我的食谱中查找所有成分。注意:这假设你有从成分回到配方的反向关系。

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Ingredient" inManagedObjectContext:self.managedObjectContext]; 
[fetchRequest setEntity:entity]; 

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"recipe == %@", self.recipe]; 
[fetchRequest setPredicate:predicate]; 

因此,childController使用此fetchRequest而不是在RootViewController中找到的NSFetchRequest。每当添加,删除或更新成分时,childController现在都会收到消息。不要忘记,childController将需要实现NSFetchedResultsControllerDelegate协议。

+0

谢谢dnorcott。我将在childController中实例化一个新的nsfetchrequest&nsfetchedresultscontroler对象。 – Leon 2010-11-13 08:29:57

相关问题