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]
韓元不要停止工作,我希望它滾動到特定的行......但我可能完全錯誤的猜測。我的腦海裏再沒有解決辦法了。請幫幫我!