This is a follow-on question from my previous one relating to why my managedObjectContext was returning to nil. I thought the direction of the question would get buried in the old one.
I now get my mangedObjectContext to not return nil and when performing [outlineView reloadData]
, nothing at all happens to my outlineView. I tried selectively removing parts of the code that are intended to update the outlineView to see if anything changes and the answer is no. I've since found out (after I wrote this question originally) that my dataSource association is disappearing at some stage.
Notes:
- the code included below is run once on awakeFromNib of the datasource class and works perfectly.
- It is on calling it at any other time that my problem arrises. I get no errors debugging but my outlineView remains unchanged.
- I narrowed this down to running on NSLog of
[outlineView dataSource]
. When the method is called fromawakeFromNib
it returns the dataSource as being my dataSourceClass correctly. Every other time it returns the dataSource as nil. - the dataSourceClass was bound to the outlineView in InterfaceBuilder.
- All other NSLog checks I've made to object updates in arrays of my code comes back as expected with correct updates. In fact, I checked the final projectsArray and clientsArray and they contain all new objects introduced before attempting to create nodes in the outlineView.
- I'm using the standard xcode generated core data app delegate code.
- I'm not using
NSTreeController
and using my ownNSOutlineViewDataSource
. It was not possible (or not documented) on how to populate an outlineView based on the parent/child relationships of two core-data entities. - I'll also include my
NSOutlineViewDataSource
code below.
Updates:
Update 1:
Hmmm... just before I call [outlineView reloadData]
I tried an NSLog
for [outlineView dataSource]
and it returned as nil. I associated the dataSource to the outlineView originally by bindings in interfaceBuilder. I'm now assuming this is my problem. Why is my datasource being released? and how do I get it back if I can't prevent it being released?
Code:
refreshOutLineView:
rootNode = [[IFParentNode alloc] initWithTitle:@"Root" children:nil];
NSInteger clientCounter;
clientCounter = 0;
NSFetchRequest *clientsFetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *clientsMoc = [clientsController managedObjectContext];
NSLog(@"clientsMoc is : %@", clientsMoc);
if(clientsMoc == nil) {
NSLog(@"And, yes, clientsMoc is = nil");
clientsMoc = [(Voiced_AppDelegate *)[[NSApplication sharedApplication] delegate] managedObjectContext];
NSLog(@"After managedObjectContext: %@", clientsMoc);
}
NSEntityDescription *clientsEntity = [NSEntityDescription entityForName:@"Clients" inManagedObjectContext:clientsMoc];
//NSLog(@"clientsEntity, after the 'if nil' code is now: %@", clientsEntity);
[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];
//NSLog(@"clientsArray, after fetching is now: %@", clientsArray);
NSInteger projectCounter;
projectCounter = 0;
NSFetchRequest *projectsFetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *projectsMoc= [projectsController managedObjectContext];
if(projectsMoc == nil) {
NSLog(@"And, yes, projectsMoc is = nil");
projectsMoc = [(Voiced_AppDelegate *)[[NSApplication sharedApplication] delegate] managedObjectContext];
NSLog(@"After managedObjectContext: %@", projectsMoc);
}
NSEntityDescription *projectsEntity = [NSEntityDescription entityForName:@"Projects" inManagedObjectContext:projectsMoc];
[projectsFetchRequest setEntity:projectsEntity];
NSError *projectsFetchError = nil;
projectsArray = [projectsMoc executeFetchRequest:projectsFetchRequest error:&projectsFetchError];
[projectsFetchRequest release];
//NSLog(@"projectsArray, after fetching is now: %@", projectsArray);
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(@"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];
[outlineView reloadData];
}
outlineViewDataSource:
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
if([item isKindOfClass:[IFChildNode class]]) {
return nil;
}
return (item == nil ? [rootNode childAtIndex:index] : [(IFParentNode *)item childAtIndex:index]);
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
return (item == nil || [item isKindOfClass:[IFParentNode class]]);
}
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
if([item isKindOfClass:[IFChildNode class]]) {
return 0;
}
return (item == nil ? [rootNode numberOfChildren] : [(IFParentNode *)item numberOfChildren]);
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
if([item isKindOfClass:[IFChildNode class]]) {
return ((IFChildNode *)item).title;
}
if([item isKindOfClass:[IFParentNode class]]) {
return ((IFParentNode *)item).title;
}
return nil;
}
// Unessential methods for datasource
- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item {
return (item == nil || [item isKindOfClass:[IFParentNode class]]);
}
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item {
return ([item isKindOfClass:[IFChildNode class]]);
}
/* - - - - - - - - - - - - - - - - - - - -
IfChild
- - - - - - - - - - - - - - - - - - - - */
@implementation IFChildNode
@synthesize title;
- (id)initWithTitle:(NSString *)theTitle {
if(self = [super init]) {
self.title = theTitle;
}
return self;
}
- (void)dealloc {
self.title = nil;
[super dealloc];
}
@end
/* - - - - - - - - - - - - - - - - - - - -
IfParent
- - - - - - - - - - - - - - - - - - - - */
@implementation IFParentNode
@synthesize title, children;
- (id)initWithTitle:(NSString *)theTitle children:(NSMutableArray *)theChildren {
if(self = [super init]) {
self.title = theTitle;
self.children = (theChildren == nil ? [NSMutableArray new] : theChildren);
}
return self;
}
- (void)addChild:(id)theChild {
[self.children addObject:theChild];
}
- (void)insertChild:(id)theChild atIndex:(NSUInteger)theIndex {
[self.children insertObject:theChild atIndex:theIndex];
}
- (void)removeChild:(id)theChild {
[self.children removeObject:theChild];
}
- (NSInteger)numberOfChildren {
return [self.children count];
}
- (id)childAtIndex:(NSUInteger)theIndex {
return [self.children objectAtIndex:theIndex];
}
- (void)dealloc {
self.title = nil;
self.children = nil;
[super dealloc];
}