1

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!

biscuitstack
  • 11,591
  • 1
  • 26
  • 41

2 Answers2

1

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.

TechZen
  • 64,370
  • 15
  • 118
  • 145
  • Thanks TechZen. No the entity is definitely there (as I said, the NSOutlineView populates ok on awakeFromNib with the code I included). It's only on calling that method later that I get that error. From more detective work I'm 99% sure that it's down to the managedObjectContext but none of the recommended solutions are doing the trick (such as referring to self first when citing MOC) or are iPhone reliant answers. – biscuitstack Mar 13 '11 at 21:34
  • Just to add: the check for managedObjectContext = nil comes back as nil so that's certainly the type of problem I'm in. It's how to solve it I'm having trouble with. I'm unsure of what aspect of the above code to leave out or add to to ensure the MOC is loading ok on calling the method multiple times. Also, I am unsure as to why it works ok on first awakening and then cuts out. – biscuitstack Mar 13 '11 at 21:36
  • It sounds like you are losing the reference to your managedObjectContext. I assume it's a property of some class such as the application delegate. Make sure the property retains the object. If you pass it along to a controller's property make sure that one retains as well. I note you don't use accessors or the `self.projectsMoc` reference form so that might be a problem as well. – TechZen Mar 14 '11 at 22:14
  • In this case there's two managedObjectContexts and they are individually properties of a `clientsController` and `projectsController` class (if I'm understanding the situation correctly - I'm still new to this - I address them with e.g.`[clientsController managedObjectContext]`. What would be the best method to check that they retain the object? I haven't altered my original xcode generated *appDelegate* for *core data*, so the managedObjectContext code for that is the standard one xcode generates if you initiate an application with *core data*. – biscuitstack Mar 14 '11 at 22:21
  • I have a feeling I might be misunderstanding a greater *managedObjectContext* beyond the *managedObjectContext* I am thinking of with each `NSArrayController`, such as when I use: `NSManagedObjectContext *clientsMoc= [clientsController managedObjectContext];` – biscuitstack Mar 14 '11 at 22:25
  • You check retention by (1) checking the property definition or (2) any custom setter/accessors you use. I presume that both controllers are getting their individual reference to the context from the app delegate? Normally, if you have the one context all references point to the same object. If the controller's is nil then you probably didn't assign/retain it properly. – TechZen Mar 15 '11 at 00:35
  • Thanks for coming back, TechZen. I was able to address some of the MOC's problem by figuring out the mac equivalent of a solution I'd seen used multiple times to solve this problem on the iPhone. If the MOC is nil, adding this line: `clientsMoc = [(nameofAppDelegate *)[[NSApplication sharedApplication] delegate] managedObjectContext];`. Getting the objects of the arrays produced from fetching the entities reveals that the new objects were inserted correctly and found their way down into the array. However, the NSOutlineView is not updating, even after reloadData using the above code? – biscuitstack Mar 15 '11 at 14:11
  • If the NSOutline view is not updating then either (1) it is not bound properly to the controller or (2) the array controller is not returning the correct objects when asked, or both. You cannot use a standard NSArrayController nor a group of NSArrayControllers to control an outline because an array controller does not understand data hierarchy. You have to use a tree controller that does understand hierarchy. Either use NSTreeController or write your own tree controller class that replicates NSTreeController behaviors. Since you are a novice, I recommend the former. – TechZen Mar 15 '11 at 17:26
  • If you can't get the NSTreeController to work, skip bindings and implement the `NSOutlineViewDataSource Protocol` in a class and control the table directly. It's more work but its way more reliable and you can actually see what is happening instead of relying on the invisible key-value observing to make things work. – TechZen Mar 15 '11 at 17:31
  • The code I included has the job of looking at 2 NSArrayControllers, finding defined values in each that refer to attributes of bound entities and sort them into parent and child nodes in an outlineView based on the parent/child relationship between both that is defined in the core-data entities. I wrote a class for this that avoids NSTreeController as it was not possible with it. In fact, the question has branched to why my outlineView won't reload so I started a new question for that at: http://stackoverflow.com/questions/5314463/why-is-reloaddata-not-refreshing-my-nsoutlineview – biscuitstack Mar 15 '11 at 17:43
  • In my code, my information filters down into two final arrays (projectsArray and clientsArray) before I commit these two being parent or child nodes in my outlineView. An NSLog of both of these reveals that any objects I've added are saved into these arrays correctly. I've introduced new information into the branched question that's more relevant to this. So I'm not sure if (2) in your possible solutions is right. I'll look into (1) but I can't see any binding problems. Thanks again TechZen. – biscuitstack Mar 15 '11 at 17:46
0

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.

Community
  • 1
  • 1
biscuitstack
  • 11,591
  • 1
  • 26
  • 41