我有一个NSOutlineView
与自定义NSOutlineViewDataSource
基于父子关系或两个核心数据实体。为什么NSManagedObjectContext = nil在第一次尝试获取它之后?
我还没有找到一种直接的方法将这些视图绑定到视图上,所以现在我想出了在向实体和它们各自的NSArrayControllers
插入一个新对象后如何告诉NSOutlineView
更新。
的NSOutlineView
上awakeFromNib
填充OK用:
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
后时间的日志(它工作正常,这一点)。我在这个网站上看到了一些关于此的提及,但提到self或NSApp代表还没有为我工作。我无法确定采取这个方向的方向?我需要返回一个不为零的MOC。
我的appdelegate类是为核心数据应用程序设置的标准类。
在此先感谢!
感谢TechZen。没有实体肯定存在(正如我所说的,NSOutlineView在包含代码的awakeFromNib上填充了ok)。只是在稍后调用该方法才会出现该错误。从更多的侦探工作中我可以肯定,这归功于managedObjectContext,但是没有一个推荐的解决方案能够做到这一点(比如引用MOC时指的是自我)或iPhone依赖的答案。 – biscuitstack
只需添加:对managedObjectContext = nil的检查返回为零,因此这当然是我遇到的问题的类型。它是如何解决我遇到的问题。我不确定上述代码的哪个方面省略或添加以确保多次调用该方法时MOC加载正常。另外,我不确定它为什么在第一次觉醒时工作正常,然后切断。 – biscuitstack
这听起来像你正在失去对managedObjectContext的引用。我认为它是某些类的属性,例如应用程序委托。确保财产保留该物品。如果您将其传递给控制器的财产,请确保其中一个也保留。我注意到你不使用访问器或'self.projectsMoc'参考表单,所以这也可能是一个问题。 – TechZen