[英]Nil value for section in Core Data
在測試了一些新功能之后,我遇到了一個Core Data錯誤,看起來確實很奇怪。
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
,我從服務器下載數據,進行解析並加載到Core Data中。 挺直的吧? 但是,每次此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]
不會停止做它的工作,而我希望它滾動到特定的行...但是我可能對此完全錯了。 我再也沒有解決方案。 請幫我!
對於您的目的,此行是錯誤的:
Activity *nearestActivity = [[Activity alloc] initWithEntity:entity insertIntoManagedObjectContext:self.moc];
因為它會將不需要的空對象插入到數據存儲中。 您應該這樣做:
Activity *nearestActivity = nil;
似乎有一個常見的誤解,您需要使用對象引用初始化指針才能使其有效,但是您不必這樣做,因為它是浪費的(在您的情況下是破壞性的)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.