为什么NSManagedObjectContext = nil在首次尝试获取后?

时间:2011-03-12 13:37:02

标签: xcode macos core-data nsoutlineview

根据父子关系或两个核心数据实体,我有NSOutlineView个自定义NSOutlineViewDataSource

我还没有找到一种直接将这些绑定到视图的方法,所以现在我正在弄清楚如何在我将一个新对象插入其中一个实体和它们之后告诉NSOutlineView更新各自NSArrayControllers

NSOutlineViewawakeFromNib上使用以下内容填充:

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之后调用它时,它等于nil(它可以正常工作)。我在这个网站上看到了一些这样的提及,但引用 self NSApp代表还没有为我工作。我对于采取何种方向毫无头绪?我需要返回一个非零的MOC。

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

提前致谢!

2 个答案:

答案 0 :(得分:1)

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

您具有错误的实体名称,或者您尚未初始化或正确引用managedObject上下文。

答案 1 :(得分:0)

我正在努力解决这个问题,这种方法在google上大量出现,涉及解决NSManagedObjectContext的{​​{1}},这似乎与iPhone有关。我按照以下方式编写了代码的mac版本:

appDelegate

调用的客户端被认为是零。但是,要找到我问题的实际答案。我不是百分百肯定,但我相信我的情况可能是由于我无意中在班上创建了两个实例,正如here指出的那样。我认为这可能是真的原因是因为我在控制台中有时会出现重复错误。我的控制器后来改变了,所以这个问题与我的项目无关。