[英]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.