2013-01-03 27 views
9

我遇到了開發我的iphone應用程序的主要問題。CoreData:錯誤:嚴重的應用程序錯誤。核心數據更改處理期間發生異常

這裏是完整的錯誤:

CoreData: error: Serious application error. Exception was caught during Core Data 
change processing. This is usually a bug within an observer of 
NSManagedObjectContextObjectsDidChangeNotification. -[TimeSpentStudying coordinate]: 
unrecognized selector sent to instance 0x21db92d0 with userInfo (null) 

這是奇怪,因爲我有兩個coreData實體(位置& TimeSpentStudying)。但我不認爲這是問題。 [TimeSpentStudying coordinate]是奇怪的,因爲我不會對TimeSpentStudying核心數據類發送的coordinate屬性不

我有一個MapView的設置,並且當用戶點按詳細公開內容按鈕上的mkannotationview,一個新的視圖(LibraryTrackTimeViewController)啪啪但是幾乎不可用。我試圖在viewDidLoad中調用NSLog並沒有顯示出來。

mapViewController.m

#pragma mark - NSNotification 

- (void)contextDidChange:(NSNotification *)notification 
{ 
    if ([self isViewLoaded]) { 
    [self updateLocations]; 
} 

- (void)updateLocations 
{ 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Location" inManagedObjectContext:self.managedObjectContext]; 

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
[fetchRequest setEntity:entity]; 

NSError *error; 
NSArray * foundObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error]; 
    if (foundObjects == nil) { 
    FATAL_CORE_DATA_ERROR(error); 
    return; 
    } 

    if (locations != nil) { 
    [self.mapView removeAnnotations:locations]; 
    } 

locations = foundObjects; 
[self.mapView addAnnotations:locations]; 

}

-(void)dealloc 
{ 
[[NSNotificationCenter defaultCenter] removeObserver:self 
    name:NSManagedObjectContextObjectsDidChangeNotification 
    object:self.managedObjectContext]; 
} 

我認爲錯誤可能在mapViewController.m

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if (distance < 500) { 
    if ([segue.identifier isEqualToString:@"TrackLibraryTimes"]) { 
    UINavigationController *navigationController = segue.destinationViewController; 

LibraryTrackTimeViewController *controller = (LibraryTrackTimeViewController *)navigationController.topViewController; 
controller.managedObjectContext = self.managedObjectContext; 
    } 
}} 

我的粗略語法道歉與prepareForSegue方法做,我只是習慣到如此,如果你需要更多的代碼,請讓我知道,謝謝大家。

+0

嗨,歡迎您!另外,很好的問題! – doge

+3

錯誤消息清楚地表明'座標'消息被髮送到'TimeSpentStudying'的一個實例,可能是地址爲0x21db92d0的對象。所以在某些時候,你正在使用一個'TimeSpentStudying'對象來預期不同的類型('Locations'?)。由於Objective-C中的動態方法解析,這些問題通常只在運行時纔會顯示出來。您應該嘗試找出問題,例如通過設置所有Objective-C異常的斷點。 –

回答

1

根據您的項目的複雜性,快速的方法來抓住這個是實現違規方法並在那裏設置斷點。當你點擊斷點時,你可以看到它被稱爲的位置,而鮑勃是你的叔叔。

所以你可以把類似

- (CLLocationCoordinate2D)coordinate{ 
    // set a breakpoint on the next line 
    NSParameterAssert(false); 
    return nil; 
} 

TimeSpentStudying類,看看它被調用。

只要確保在完成後刪除它。

-3

我會嘗試的東西的組合。

  1. 從模擬器中刪除應用程序並清理應用程序並再次運行。

  2. 看着你的代碼,我覺得你正在將managedContextObject從一個控制器傳遞到另一個控制器,不需要那樣做。用戶AppDelegate sharedInstance可以隨時獲得managedObject。它不會創建多個實例,它是一個單例對象。

試試這兩個,希望你可能會找到一些答案或可能會解決它。

+1

投票下來的B/C你實際上從來不想用Core Data來做這件事,並且總是使用蘋果公司推薦的「傳遞接力棒」的方式傳遞MOC。 – bpapa

4

我有同樣的錯誤,由於

-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath 

case NSFetchedResultsChangeInsert: 
      //[self.tableView insert 
      NSLog(@"change insert"); 
      [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
      break; 

我已經使用indexPath代替newIndexPath。

+0

謝謝道格,你救了我的培根 –

2

通過取消分配提取的結果控制器來解決該問題。它仍然執行提取,因爲我在單獨的屏幕中添加/編輯對象。

相關問題