1

我有一个基于父子关系或两个核心数据实体NSOutlineView的自定义。NSOutlineViewDataSource

我还没有找到一种直接的方法将这些绑定到视图,所以现在我正在弄清楚如何NSOutlineView在我将新对象插入任何一个实体及其各自的NSArrayControllers.

NSOutlineView填充awakeFromNib确定:

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的日志显示,每次我调用它之后它都等于 nil awakefromnib(它适用于此)。我在这个网站上看到过一些关于此的提及,但引用selfNSApp 代表还没有为我工作。我不知道该采取什么方向?我需要返回一个不为零的 MOC。

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

提前致谢!

4

2 回答 2

1

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

要么您的实体名称错误,要么您的 managedObject 上下文未初始化或未正确引用。

于 2011-03-13T20:46:49.187 回答
0

我正在努力使用在谷歌上出现很多涉及解决似乎总是与 iPhone 相关的NSManagedObjectContext问题的方法来解决这个问题。appDelegate我按照以下方式制作了代码的mac版本:

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

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

于 2011-03-18T13:18:13.250 回答