2014-04-07 32 views
1

經過一些測試新功能後,我遇到了一個看起來很奇怪的核心數據錯誤。核心數據部分的無值

CoreData: error: (NSFetchedResultsController) A section returned nil value for section name key path 'date'. Objects will be placed in unnamed section 

如果我滾動到我UITableView我看到它NULL值單元格頂部...


讓我給你介紹我的情況。在UITableView的第一個(有史以來)viedDidLoad中,我從服務器下載數據,解析並加載到核心數據中。挺直的,是吧?但每次這個viewDidLoad我檢查是否有東西已經存儲在這個實體,如果是這樣我滾動到最近的日期到目前的日期行。爲了實現我寫這個,我使用方法:

- (void)scrollToNearestDateInFutureWithAnimation:(BOOL)animation 
{ 
    // Saving present date for comparison purposes 
    NSDate *presentDate = [NSDate date]; 
    // Making a big interval (2001) 
    double interval = fabs([NSDate timeIntervalSinceReferenceDate]); 

    // Getting managed object to hold the proper row 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Activity" inManagedObjectContext:self.moc]; 
    Activity *nearestActivity = [[Activity alloc] initWithEntity:entity insertIntoManagedObjectContext:self.moc]; 

    // Iterating each activity in fetched objects 
    for (Activity *activity in [self.fetchedResultsController fetchedObjects]) { 
     // Getting time interval between present date and activity start date 
     double activityIntervalSincePresent = [activity.startDate timeIntervalSinceDate:presentDate]; 

     // Checing if interval is smaller than default one and is bigger than 0 (eliminating past dates) 
     if (interval > activityIntervalSincePresent && activityIntervalSincePresent > 0) { 
      interval = activityIntervalSincePresent; 
      nearestActivity = activity; 
     } 
    } 

    // Scrolling to the row 
    [self.tableView scrollToRowAtIndexPath:[self.fetchedResultsController indexPathForObject:nearestActivity] atScrollPosition:UITableViewScrollPositionTop animated:animation]; 
} 

viewDidLoad負責檢驗實體的核心數據的所有腦幹部分:

if ([self coreDataHasEntriesForEntityName:@"Timetable"]) { 
     // NSLog(@"Core Data has entries"); 
     NSError *error; 
     if (![[self fetchedResultsController] performFetch:&error]) { 
      // Update to handle the error appropriately. 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     } 

     // Scrolling to row with nearest date in future 
     [self scrollToNearestDateInFutureWithAnimation:NO]; 

     // Checking for new timetable 
     [self checkForTimetableUpdatesWithVersions:[self getTimetableVersions]]; 
    } else { 
     // NSLog(@"No entries in Core Data"); 
     if ([self registeredTimetableExistsInUserDefaults]) { 
      self.timetableID = [[NSUserDefaults standardUserDefaults] valueForKey:TIMETABLE_ID]; 
      [self showDownloadTimetableActionSheet]; 
     } else { 
      [self showRegisterTimetableActionSheet]; 
     } 
    } 

對我來說,這似乎是有時[[self fetchedResultsController] performFetch:&error]韓元不要停止工作,我希望它滾動到特定的行......但我可能完全錯誤的猜測。我的腦海裏再沒有解決辦法了。請幫幫我!

回答

2

此行是錯誤的,你的目的:

Activity *nearestActivity = [[Activity alloc] initWithEntity:entity insertIntoManagedObjectContext:self.moc]; 

,因爲它插入一個空的對象到您不希望數據存儲。你應該做:

Activity *nearestActivity = nil; 

似乎是你需要初始化一個指針與對象引用它是有效的一種常見的誤解,但你沒有,所以不要,因爲這是浪費(並在你的情況下是腐敗)