将ManagedObjectContext传递到第二个视图 [英] Passing a ManagedObjectContext to a second view

查看:82
本文介绍了将ManagedObjectContext传递到第二个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写我的第一个iPhone / Cocoa应用程序。它在导航视图内有两个表视图。当您触摸第一个表视图中的行时,将转到第二个表视图。我想第二个视图显示来自CoreData实体的记录,这些记录与第一个视图中触及的行相关。



我有CoreData数据在第一个视图中显示得很好表视图。您可以触摸一行并转到第二个表视图。我能够将信息从所选对象从第一个视图传递到第二个视图。但我不能得到第二个视图做自己的CoreData抓取。对于我的生活我不能得到managedObjectContext对象传递到第二个视图控制器。我不想在第一个视图中执行查找,并传递一个字典,因为我想要能够使用搜索字段来细化第二个视图中的结果,以及从中插入新的条目到CoreData数据。 / p>

这是从第一个视图转换到第二个视图的函数。

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//导航逻辑可以在这里 - 例如,创建和推送另一个视图控制器。
NSManagedObject * selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
SecondViewController * secondViewController = [[SecondViewController alloc] initWithNibName:@SecondViewbundle:nil];

secondViewController.tName = [[selectedObject valueForKey:@name] description];
secondViewController.managedObjectContext = [self managedObjectContext];

[self.navigationController pushViewController:secondViewController animated:YES];
[secondViewController release];
}

这是SecondViewController中的函数崩溃:

   - (void)viewDidLoad {
[super viewDidLoad];

self.title = tName;

NSError * error;
if(![[self fetchedResultsController] performFetch:& error]){//< - crashes here
//处理错误...
}
}

- (NSFetchedResultsController *)fetchedResultsController {

if(fetchedResultsController!= nil){
return fetchedResultsController;
}

/ *
设置获取的结果控制器。
* /
//为实体创建获取请求。
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
//根据需要编辑实体名称。
// ****在下一行崩溃,因为managedObjectContext == 0x0
NSEntityDescription * entity = [NSEntityDescription entityForName:@SecondEntityinManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

//< snip> ...更多代码在这里从苹果模板,从来没有被执行,因为崩溃

return fetchedResultsController;
}

我在这里做错了什么想法?



managedObjectContext是一个保留属性。



更新:我插入了一个NSLog([[managedObjectContext registeredObjects] description] );在viewDidLoad和它似乎managedObjectContext正在传递就好了。



由于未捕获异常NSInternalInconsistencyException而终止应用程序,原因:'+ entityForName:找不到实体名称的NSManagedObjectModel'SecondEntity' p>

解决方案

哦,这很有趣。



因此,pushViewController不会一次调用viewDidLoad,而是两次调用viewDidLoad两次 >。第一次调用viewDidLoad时,对象似乎没有正确实例化。第二次,他们是。因此,第一次运行此代码时,无法访问managedObjectContext,它会抛出异常。第二次运行,一切都很好。没有崩溃。



有很多关于viewDidLoad在Google上执行多次的问题的引用,所以我认为解决方案是不在viewDidLoad中执行此获取请求初始化。


I'm writing my first iPhone/Cocoa app. It has two table views inside a navigation view. When you touch a row in the first table view, you are taken to the second table view. I would like the second view to display records from the CoreData entities related to the row you touched in the first view.

I have the CoreData data showing up fine in the first table view. You can touch a row and go to the second table view. I'm able to pass info from the selected object from the first to the second view. But I cannot get the second view to do its own CoreData fetching. For the life of me I cannot get the managedObjectContext object to pass to the second view controller. I don't want to do the lookups in the first view and pass a dictionary because I want to be able to use a search field to refine results in the second view, as well as insert new entries to the CoreData data from there.

Here's the function that transitions from the first to the second view.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here -- for example, create and push another view controller.
    NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
    SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];

    secondViewController.tName = [[selectedObject valueForKey:@"name"] description];
    secondViewController.managedObjectContext = [self managedObjectContext];

    [self.navigationController pushViewController:secondViewController animated:YES];
    [secondViewController release];
}

And this is the function inside SecondViewController that crashes:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = tName;

    NSError *error;
    if (![[self fetchedResultsController] performFetch:&error]) { // <-- crashes here
        // Handle the error...
    }
}

- (NSFetchedResultsController *)fetchedResultsController {

    if (fetchedResultsController != nil) {
        return fetchedResultsController;
    }

    /*
     Set up the fetched results controller.    
     */
    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
        // **** crashes on the next line because managedObjectContext == 0x0
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"SecondEntity" inManagedObjectContext:managedObjectContext]; 
    [fetchRequest setEntity:entity];

    // <snip> ... more code here from Apple template, never gets executed because of the crashing

    return fetchedResultsController;
}

Any ideas on what I am doing wrong here?

managedObjectContext is a retained property.

UPDATE: I inserted a NSLog([[managedObjectContext registeredObjects] description]); in viewDidLoad and it appears managedObjectContext is being passed just fine. Still crashing, though.

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'SecondEntity''

解决方案

Oh, this is interesting. I spent some quality time with the stack trace and I think I've got it figured out.

So pushViewController calls viewDidLoad not once, but twice. The first time it calls viewDidLoad, the objects appear to not be instantiated properly. The second time, they are. So the first time this code runs it can't access managedObjectContext and it throws an exception. The second time it runs, everything's fine. No crash.

There are a lot of references to problems with viewDidLoad executing multiple times on Google, so I think the solution is to not do this fetch request initialization in viewDidLoad.

这篇关于将ManagedObjectContext传递到第二个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆