TabController中的第二个表视图 - 崩溃

时间:2011-12-07 18:17:16

标签: iphone uitableview nsfetchedresultscontroller uitabcontroller

更新 这是一种复制问题的简单方法,打开任何带有从核心数据加载的tableview的应用程序。然后在选项卡栏中,设置不同的选项卡以打开相同的tableview。你得到*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'FooSpelledCorrectly''

我在tabcontroller应用程序中有第二个表视图,当选中该选项卡时应用程序崩溃。我得SIGABRT i like frogs

我复制了tableview的(h | m)文件并将它们添加到项目中。在应用委托中,我添加了SearchGroupViewController *searchListController2;,原始的表格视图使用了SearchDestinationsViewController *searchListController;

我觉得我必须遗漏一些简单的东西。还有什么想法可以看吗?我需要创建第二个控制器吗? 两个.m文件都实现了fetchedResultsController

- (NSFetchedResultsController *)fetchedResultsController {

    // Set up the fetched results controller if needed.
    if (fetchedResultsController == nil) {
        // Create the fetch request for the entity.
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        // Edit the entity name as appropriate.
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"FooSpelledCorrectly" inManagedObjectContext:managedObjectContext];
        [fetchRequest setEntity:entity];

        NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"state" ascending:YES];// was name

        NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];

        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1,sortDescriptor2, nil];// was 2// sortDescriptor,

        [fetchRequest setSortDescriptors:sortDescriptors];

        // Edit the section name key path and cache name if appropriate.
        // nil for section name key path means "no sections".
        NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"state" cacheName:nil];//@"state"
        aFetchedResultsController.delegate = self;
        self.fetchedResultsController = aFetchedResultsController;

        //letters = [aFetchedResultsController valueForKey:@"alphabetIndex"];

        [aFetchedResultsController release];
        [fetchRequest release];
        //[sortDescriptor release];
        [sortDescriptor1 release];
        [sortDescriptor2 release];
        [sortDescriptors release];
    }

崩溃日志:

2011-12-07 13:11:46.367 CoveredBridges[5762:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Recipe''
*** Call stack at first throw:

感谢您的帮助或指示! 罗伯特

1 个答案:

答案 0 :(得分:0)

简而言之,答案是在每个视图中实例化数据,并在viewDidLoad完成后释放它。之前正在从代表处加载和共享应用程序。也许我可以检查存在吗?它现在有效,我想分享。

managedObjectContext = nil;
managedObjectContext = [(RecipesAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyAwesomeData" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"state" ascending:YES];

        NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];

    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1,sortDescriptor2, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

        NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"state" cacheName:nil];
        aFetchedResultsController.delegate = self;
        self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor1 release];
    [sortDescriptor2 release];
    [sortDescriptors release];