2011-03-12 39 views
1

我有一个NSOutlineView与自定义NSOutlineViewDataSource基于父子关系或两个核心数据实体。为什么NSManagedObjectContext = nil在第一次尝试获取它之后?

我还没有找到一种直接的方法将这些视图绑定到视图上,所以现在我想出了在向实体和它们各自的NSArrayControllers插入一个新对象后如何告诉NSOutlineView更新。

NSOutlineViewawakeFromNib填充OK用:

rootNode = [[IFParentNode alloc] initWithTitle:@"Root" children:nil]; 
    NSInteger clientCounter; 
    clientCounter = 0; 
    NSFetchRequest *clientsFetchRequest = [[NSFetchRequest alloc] init]; 
    NSManagedObjectContext *clientsMoc= [clientsController managedObjectContext]; 
    NSEntityDescription *clientsEntity = [NSEntityDescription entityForName:@"Clients" inManagedObjectContext:clientsMoc]; 
    [clientsFetchRequest setEntity:clientsEntity]; 
    //sort 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"clientCompany" ascending:YES]; 
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 
    [clientsFetchRequest setSortDescriptors:sortDescriptors]; 
    NSError *clientsFetchError = nil; 
    clientsArray = [clientsMoc executeFetchRequest:clientsFetchRequest error:&clientsFetchError]; 
    [clientsFetchRequest release]; 

    NSInteger projectCounter; 
    projectCounter = 0; 
    NSFetchRequest *projectsFetchRequest = [[NSFetchRequest alloc] init]; 
    NSManagedObjectContext *projectsMoc= [projectsController managedObjectContext]; 
    NSEntityDescription *projectsEntity = [NSEntityDescription entityForName:@"Projects" inManagedObjectContext:projectsMoc]; 
    [projectsFetchRequest setEntity:projectsEntity]; 
    NSError *projectsFetchError = nil; 
    projectsArray = [projectsMoc executeFetchRequest:projectsFetchRequest error:&projectsFetchError]; 
    [projectsFetchRequest release]; 

    for (NSString *s in clientsArray) { 
     NSManagedObject *clientMo = [clientsArray objectAtIndex:clientCounter]; // assuming that array is not empty 
     id clientValue = [clientMo valueForKey:@"clientCompany"]; 
     //NSLog(@"Company is %@", parentValue); 

     IFParentNode *tempNode = [[IFParentNode alloc] initWithTitle:[NSString stringWithFormat:@"%@", clientValue] children:nil]; 

     clientCounter = clientCounter + 1; 
     [rootNode addChild:tempNode]; 
     [tempNode release]; 
    } 

    for (NSString *s in projectsArray) { 
     NSInteger viewNodeIndex; 
     viewNodeIndex = 0; 
     NSManagedObject *projectMo = [projectsArray objectAtIndex:projectCounter]; // assuming that array is not empty 
     id projectValue = [projectMo valueForKey:@"projectTitle"]; 
     id projectParent = [[projectMo valueForKey:@"projectParent"] valueForKey: @"clientCompany"]; 
     // find if theres an item with the projetParent name 
     id nodeTitle = [[rootNode children] valueForKey:@"title"]; 
     for(NSString *companies in nodeTitle) { 
      if([companies compare:projectParent] == NSOrderedSame) { 
       //NSLog(@"Yeh! Company is %@ and parent is %@ and id is: %d", companies, projectParent, viewNodeIndex); 
       // then assign that node to be the tempnode. 
       IFParentNode *tempNode = [rootNode.children objectAtIndex:viewNodeIndex]; 
       IFChildNode *subTempNode = [[IFChildNode alloc] initWithTitle:[NSString stringWithFormat:@"%@", projectValue]]; 
       [tempNode addChild:subTempNode]; 
       [subTempNode release]; 
       [tempNode release]; 
      } else { 
       // do nothing. 
      } 
      viewNodeIndex = viewNodeIndex + 1; 
     } 
     projectCounter = projectCounter + 1; 
    } 

    [outlineView expandItem:nil expandChildren:YES]; 

我想每一个我添加了一个对象要么实体时调用此相同的方法,以为它会再次填充NSOutlineView从头开始。相反,它只是提供了一个错误:

+entityForName: could not locate an NSManagedObjectModel for entity name 'Clients' 

clientsMoc表明,它是等于零的每一个我称之为awakefromnib后时间的日志(它工作正常,这一点)。我在这个网站上看到了一些关于此的提及,但提到selfNSApp代表还没有为我工作。我无法确定采取这个方向的方向?我需要返回一个不为零的MOC。

我的appdelegate类是为核心数据应用程序设置的标准类。

在此先感谢!

回答

0

我一直在努力解决这个问题,这个方法在谷歌上出现了很多问题,涉及到appDelegateNSManagedObjectContext,这似乎总是与iPhone相关。我做了一个mac版本的代码沿线︰

clientsMoc = [(nameofAppDelegate *)[[NSApplication sharedApplication] delegate] managedObjectContext]; 

这被称为clientsMoc被发现是零。但是,我的问题得到了实际答案。我不是100%确定的,但我相信在我的情况下,可能是由于在我的班级中无意中创建了两个实例,正如here所指出的那样。我认为这可能是真实的原因是因为我有时在控制台中出现重复错误。我的控制器后来改变了,所以这个问题变得与我的项目无关。

1

您报告的错误与大纲无关。这是一个核心数据错误。

要么你有错误的实体名称,要么你的managedObject上下文没有被初始化或被正确引用。

+0

感谢TechZen。没有实体肯定存在(正如我所说的,NSOutlineView在包含代码的awakeFromNib上填充了ok)。只是在稍后调用该方法才会出现该错误。从更多的侦探工作中我可以肯定,这归功于managedObjectContext,但是没有一个推荐的解决方案能够做到这一点(比如引用MOC时指的是自我)或iPhone依赖的答案。 – biscuitstack

+0

只需添加:对managedObjectContext = nil的检查返回为零,因此这当然是我遇到的问题的类型。它是如何解决我遇到的问题。我不确定上述代码的哪个方面省略或添加以确保多次调用该方法时MOC加载正常。另外,我不确定它为什么在第一次觉醒时工作正常,然后切断。 – biscuitstack

+0

这听起来像你正在失去对managedObjectContext的引用。我认为它是某些类的属性,例如应用程序委托。确保财产保留该物品。如果您将其传递给控制器​​的财产,请确保其中一个也保留。我注意到你不使用访问器或'self.projectsMoc'参考表单,所以这也可能是一个问题。 – TechZen

相关问题