为什么第一次尝试获取 NSManagedObjectContext = nil ?

发布于 2024-10-21 18:18:53 字数 4149 浏览 5 评论 0原文

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

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

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(它对此工作正常)。我在这个网站上看到过一些关于这一点的提及,但引用 selfNSApp delegates 还没有为我工作。我不知道该朝什么方向发展?我需要返回一个不为零的 MOC。

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

提前致谢!

I have an NSOutlineView with a custom NSOutlineViewDataSource based on the parent-child relationship or two core data entities.

I haven't found a straightforward way to bind these to the view yet so for now I'm figuring out how to tell the NSOutlineView to update after I insert a new object into either of the entities and their respective NSArrayControllers.

The NSOutlineView populates ok on awakeFromNib with:

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];

I tried calling this same method each time I added an object to either entity, thinking it would populate the NSOutlineView from scratch again. Instead, it just gives an error:

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

A log of clientsMoc reveals that it's equal to nil for every time I call it after awakefromnib (it works fine for this). I've seen a few mentions of this on this site but referencing self or NSApp delegates haven't worked for me yet. I'm clueless as to what direction to take this? I need to return a MOC that isn't nil.

My appdelegate class is the standard one set up for core data applications.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月无声 2024-10-28 18:18:53

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

您的实体名称错误,或者托管对象上下文尚未初始化或正确引用。

The error you report has nothing to do with the outline. It's a Core Data error.

Either you have the wrong entity name or you managedObject context has not been initialized or properly referred to.

上课铃就是安魂曲 2024-10-28 18:18:53

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

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

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

I was struggling to fix this using the method that crops up a lot on google involving addressing the NSManagedObjectContext of the appDelegate that always seems to be relevant to the iPhone. I made a mac version of the code along the lines of:

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

That was called of the clientsMoc was found to be nil. Getting to the actual answer to my question, though. I'm not 100% sure but I believe in my case it may have been down to me having inadvertently created two instances in my class, as pointed out here. The reason I think this may be true is because I was getting duplicate errors at times in the console. My controller later changed so this question became irrelevant to my project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文