简体   繁体   中英

Nil value for section in Core Data

After some testing new features I faced a Core Data error which was really strange looking.

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

And if I scroll to the top of my UITableView I see cell with NULL values in it...


Let me introduce you to my situation. On the first (ever) viedDidLoad of this UITableView I download data from the server, parse it and load into Core Data. Pretty straight, huh? But everytime this viewDidLoad I check if there is something already stored in this entity and if so I scroll to the row which has nearest date to present date. To accomplish this I use method I've written:

- (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];
}

And a part of viewDidLoad responsible for checking the existance of entity in Core Data:

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];
        }
    }

For me, it seems like sometimes [[self fetchedResultsController] performFetch:&error] won't stop doing its job and I want it to scroll to the specific row... But I may be totally wrong with this guess. No more solutions come to my mind. Please help me!

This line is wrong for your purposes:

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

because it is inserting an empty object into the data store that you don't want. You should just be doing:

Activity *nearestActivity = nil;

There seems to be a common misconception that you need to initialise a pointer with an object reference for it to be valid, but you don't, so don't, because it's wasteful (and in your case corruptive)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM