2011-03-12 40 views
1

我有一個NSOutlineView與自定義NSOutlineViewDataSource基於父子關係或兩個核心數據實體。爲什麼NSManagedObjectContext = nil在第一次嘗試獲取它之後?

我還沒有找到一種直接的方法將這些視圖綁定到視圖上,所以現在我想出了在向實體和它們各自的NSArrayControllers插入一個新對象後如何告訴NSOutlineView更新。

NSOutlineViewawakeFromNib填充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後時間的日誌(它工作正常,這一點)。我在這個網站上看到了一些關於此的提及,但提到selfNSApp代表還沒有爲我工作。我無法確定採取這個方向的方向?我需要返回一個不爲零的MOC。

我的appdelegate類是爲核心數據應用程序設置的標準類。

在此先感謝!

回答

0

我一直在努力解決這個問題,這個方法在谷歌上出現了很多問題,涉及到appDelegateNSManagedObjectContext,這似乎總是與iPhone相關。我做了一個mac版本的代碼沿線︰

clientsMoc = [(nameofAppDelegate *)[[NSApplication sharedApplication] delegate] managedObjectContext]; 

這被稱爲clientsMoc被發現是零。但是,我的問題得到了實際答案。我不是100%確定的,但我相信在我的情況下,可能是由於在我的班級中無意中創建了兩個實例,正如here所指出的那樣。我認爲這可能是真實的原因是因爲我有時在控制檯中出現重複錯誤。我的控制器後來改變了,所以這個問題變得與我的項目無關。

1

您報告的錯誤與大綱無關。這是一個核心數據錯誤。

要麼你有錯誤的實體名稱,要麼你的managedObject上下文沒有被初始化或被正確引用。

+0

感謝TechZen。沒有實體肯定存在(正如我所說的,NSOutlineView在包含代碼的awakeFromNib上填充了ok)。只是在稍後調用該方法纔會出現該錯誤。從更多的偵探工作中我可以肯定,這歸功於managedObjectContext,但是沒有一個推薦的解決方案能夠做到這一點(比如引用MOC時指的是自我)或iPhone依賴的答案。 – biscuitstack

+0

只需添加:對managedObjectContext = nil的檢查返回爲零,因此這當然是我遇到的問題的類型。它是如何解決我遇到的問題。我不確定上述代碼的哪個方面省略或添加以確保多次調用該方法時MOC加載正常。另外,我不確定它爲什麼在第一次覺醒時工作正常,然後切斷。 – biscuitstack

+0

這聽起來像你正在失去對managedObjectContext的引用。我認爲它是某些類的屬性,例如應用程序委託。確保財產保留該物品。如果您將其傳遞給控制器​​的財產,請確保其中一個也保留。我注意到你不使用訪問器或'self.projectsMoc'參考表單,所以這也可能是一個問題。 – TechZen

相關問題