核心数据中的部分为零值

时间:2014-04-07 20:31:55

标签: ios iphone objective-c uitableview 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值的单元格...


让我向您介绍我的情况。在这个viedDidLoad的第一个(永远)UITableView我从服务器下载数据,解析它并加载到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]不会停止做它的工作而我希望它滚动到特定的行...但我可能完全错误的猜测。我想不到更多的解决方案。请帮帮我!

1 个答案:

答案 0 :(得分:2)

这条线对你的目的是错误的:

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

因为它将一个空对象插入到您不想要的数据存储中。你应该这样做:

Activity *nearestActivity = nil;

似乎有一种常见的误解,你需要初始化一个带有对象引用的指针才能使它有效,但是你没有,所以不要,因为它是浪费的(在你的情况下是腐败的) )