我有一個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