biscuitstack
biscuitstack

Reputation: 12132

Why is NSManagedObjectContext = nil after first attempt to fetch it?

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!

Upvotes: 1

Views: 1580

Answers (2)

biscuitstack
biscuitstack

Reputation: 12132

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.

Upvotes: 0

TechZen
TechZen

Reputation: 64428

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.

Upvotes: 1

Related Questions