这是我之前关于我的 managedObjectContext 返回nil的原因的后续问题。我认为问题的方向会被埋没在旧问题中。
我现在让我的 mangedObjectContext 不返回nil,而在执行[outlineView reloadData]
时,我的outlineView什么都没发生。我尝试选择性地删除了用于更新 outlineView 的部分代码,以查看是否有任何更改,答案是否定的。我已经发现(在我最初写这个问题之后)我的dataSource关联在某个阶段消失了。
[outlineView dataSource]
的NSLog上运行。从awakeFromNib
调用该方法时,它将dataSource正确地返回为我的dataSourceClass。每隔一次它将dataSource作为 nil 返回。NSTreeController
并使用我自己的NSOutlineViewDataSource
。关于如何根据两个核心数据实体的父/子关系填充outlineView是不可能的(或没有记录)。NSOutlineViewDataSource
代码。 更新1:
嗯...就在我致电[outlineView reloadData]
之前,我为NSLog
尝试了[outlineView dataSource]
,然后又返回 nil 。我最初通过 interfaceBuilder 中的绑定将 dataSource 与outlineView相关联。我现在假设这是我的问题。为什么要发布我的数据源?如果我不能阻止它被释放,我该如何取回它呢?
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];
}
答案 0 :(得分:1)
大多数情况下,这意味着您无意中创建了类的两个实例。一个在你的笔尖中并且连接到所有东西,而另一个是在代码中创建的,或者在没有任何连接的情况下在笔尖中的其他地方创建。证明这种情况发生的一种简单方法是在self
中记录awakeFromNib
和一些看到nil的方法 - 您将看到两个对象的不同地址。你还会发现outlineView
本身是零,因为出口没有连接到任何东西。
答案 1 :(得分:0)
在我看来,您正在对控制器中的数据强加树形结构,而不是在数据模型中。您不必将两个实体提取到两个数组中,然后将它们组合在一起以创建树。绑定背后的想法是控制器只是将数据模型链接到控件,控件在数据模型中显示对象图。
所以,我认为您需要备份并再次查看您的数据模型。我推断你需要一个像这样的数据模型:
Client{
name:string
//... some other attributes
projects<-->>Project.client
}
Project:
name:string
// ... some other attributes
client<<-->Client.projects
}
您需要一个大纲,其中显示客户端及其相关项目对象为子项。
如果您使用NSTreeController,您可以将树控制器绑定到子路径为projects
如果您使用大纲数据源,您只需获取按照您的意愿排序的客户端对象,然后根据需要返回它们及其项目。
这里重要的是大纲或树结构应该是数据模型本身的先天。如果不是,那么您可能不希望用户以大纲形式查看和思考数据。
请记住,数据模型是模型 - 视图 - 控制器设计的整个层,并不仅仅是一个愚蠢的位存储。它可以而且应该包含表示应用程序中活动数据所需的所有逻辑。可以说,数据模型是应用程序的核心,可根据需要添加UI,网络或持久性。不要害怕在数据模型中放入与数据相关的逻辑。