gpt4 book ai didi

ios - 核心数据部分的零值

转载 作者:行者123 更新时间:2023-11-29 10:44:22 24 4
gpt4 key购买 nike

在测试了一些新功能之后,我遇到了一个 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 的一部分负责检查 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];
}
}

对我来说,似乎有时 [[self fetchedResultsController] performFetch:&error] 不会停止工作,我希望它滚动到特定的行......但我可能是这个猜测完全错误。我没有想到更多的解决方案。请帮助我!

最佳答案

这一行对于您的目的是错误的:

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

因为它向数据存储中插入了一个您不想要的空对象。你应该只是做:

Activity *nearestActivity = nil;

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

关于ios - 核心数据部分的零值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22922490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com