NSFetchedResultsController 不会根据请求重新加载数据
NSFetchedResultsController does not reload data on request
希望有人可以向我解释为什么我的 NSFetchedResultsController 只获取一次数据。
我创建了一个带有 NSFetchedResultsController 方法的单例。我的应用程序有一个 UITabBarController,其中一个选项卡中有一个 UINavigationController
。在这个 UINavigationController
中,我设置了一个 UIScrollView
和 3 个表格视图,全部来自 1 UITableViewController
。它们的不同之处在于一个显示按天分组的数据,一个按周分组,一个按月分组。
我的问题是,在为滚动视图初始化三个视图后,我的 NSFetchedResultController 没有为每个视图获取数据。它只触发一次然后停止工作。在视图之间滑动时,我在 tableview 上重新加载数据,但没有任何结果。
我注意到当我重新加载时,这段代码被多次调用。
if (_fetchedResultsController != nil)
{
return _fetchedResultsController;
}
我尝试 运行 没有此代码的代码,但随后我的 _fetchedResultsController 将 return NULL。
任何想法如何让这个工作?
我的代码:
日期列表视图
// Views for scrolling view
// Daily view
_dateListByDayVC = [[BITDateListViewController alloc]init];
_dateListByDayVC.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
[self addChildViewController:_dateListByDayVC];
[_scrollView addSubview:_dateListByDayVC.view];
// Weekly view
_dateListByWeekVC = [[BITDateListViewController alloc]init];
_dateListByWeekVC.view.frame = CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height);
[self addChildViewController:_dateListByWeekVC];
[_scrollView addSubview:_dateListByWeekVC.view];
// Monthly view
_dateListByMonthVC = [[BITDateListViewController alloc]init];
_dateListByMonthVC.view.frame = CGRectMake(self.view.bounds.size.width *2, 0, self.view.bounds.size.width, self.view.bounds.size.height);
[self addChildViewController:_dateListByMonthVC];
[_scrollView addSubview:_dateListByMonthVC.view];
单例
// Setup init for fetchcontroller
-(void)doFetchForResultController
{
NSError *error;
if (![_fetchedResultsController performFetch:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
}
// Setup fetchedResultController for use of dynamic sections
- (NSFetchedResultsController *)fetchedResultsController:(NSUInteger)index;
{
if (_fetchedResultsController != nil)
{
return _fetchedResultsController;
}
// Clean the cache
[NSFetchedResultsController deleteCacheWithName:@"Root"];
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entries" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Sort using the timeStamp property.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date"
ascending:YES];
[fetchRequest setSortDescriptors:@[sortDescriptor]];
NSLog(@"fetchedResultController %ld", index);
if (index == 0) {
NSLog(@"fetchedResultsController index == 0");
// Use the sectionIdentifier property to group into sections.
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:context
sectionNameKeyPath:@"dateAsSectionName"
cacheName:@"Root"];
}
else if (index == 1) {
NSLog(@"fetchedResultsController index == 1");
// Use the sectionIdentifier property to group into sections.
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:context
sectionNameKeyPath:@"weekAsSectionName"
cacheName:@"Root"];
}
else if (index == 2) {
NSLog(@"fetchedResultsController index == 2");
// Use the sectionIdentifier property to group into sections.
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:context
sectionNameKeyPath:@"monthAsSectionName"
cacheName:@"Root"];
}
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
你的问题基本上是你试图重复使用 1 FRC 但你从未销毁和重新配置它。
将一个 FRC 保持在一个单独的实例中并尝试重新配置它并没有太多好处,它甚至不是共享状态。
最好让每个视图控制器创建和维护自己的 FRC,这样您就不必担心销毁和共享问题。内存不应该是一个问题,因为 FRC 本身是最小的,并且每个都会出错需要托管对象(一定要在你的获取请求上设置批处理大小以提高它们的效率,就像你目前拥有的那样:-))。
希望有人可以向我解释为什么我的 NSFetchedResultsController 只获取一次数据。
我创建了一个带有 NSFetchedResultsController 方法的单例。我的应用程序有一个 UITabBarController,其中一个选项卡中有一个 UINavigationController
。在这个 UINavigationController
中,我设置了一个 UIScrollView
和 3 个表格视图,全部来自 1 UITableViewController
。它们的不同之处在于一个显示按天分组的数据,一个按周分组,一个按月分组。
我的问题是,在为滚动视图初始化三个视图后,我的 NSFetchedResultController 没有为每个视图获取数据。它只触发一次然后停止工作。在视图之间滑动时,我在 tableview 上重新加载数据,但没有任何结果。
我注意到当我重新加载时,这段代码被多次调用。
if (_fetchedResultsController != nil)
{
return _fetchedResultsController;
}
我尝试 运行 没有此代码的代码,但随后我的 _fetchedResultsController 将 return NULL。 任何想法如何让这个工作?
我的代码:
日期列表视图
// Views for scrolling view
// Daily view
_dateListByDayVC = [[BITDateListViewController alloc]init];
_dateListByDayVC.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
[self addChildViewController:_dateListByDayVC];
[_scrollView addSubview:_dateListByDayVC.view];
// Weekly view
_dateListByWeekVC = [[BITDateListViewController alloc]init];
_dateListByWeekVC.view.frame = CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height);
[self addChildViewController:_dateListByWeekVC];
[_scrollView addSubview:_dateListByWeekVC.view];
// Monthly view
_dateListByMonthVC = [[BITDateListViewController alloc]init];
_dateListByMonthVC.view.frame = CGRectMake(self.view.bounds.size.width *2, 0, self.view.bounds.size.width, self.view.bounds.size.height);
[self addChildViewController:_dateListByMonthVC];
[_scrollView addSubview:_dateListByMonthVC.view];
单例
// Setup init for fetchcontroller
-(void)doFetchForResultController
{
NSError *error;
if (![_fetchedResultsController performFetch:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
}
// Setup fetchedResultController for use of dynamic sections
- (NSFetchedResultsController *)fetchedResultsController:(NSUInteger)index;
{
if (_fetchedResultsController != nil)
{
return _fetchedResultsController;
}
// Clean the cache
[NSFetchedResultsController deleteCacheWithName:@"Root"];
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entries" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Sort using the timeStamp property.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date"
ascending:YES];
[fetchRequest setSortDescriptors:@[sortDescriptor]];
NSLog(@"fetchedResultController %ld", index);
if (index == 0) {
NSLog(@"fetchedResultsController index == 0");
// Use the sectionIdentifier property to group into sections.
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:context
sectionNameKeyPath:@"dateAsSectionName"
cacheName:@"Root"];
}
else if (index == 1) {
NSLog(@"fetchedResultsController index == 1");
// Use the sectionIdentifier property to group into sections.
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:context
sectionNameKeyPath:@"weekAsSectionName"
cacheName:@"Root"];
}
else if (index == 2) {
NSLog(@"fetchedResultsController index == 2");
// Use the sectionIdentifier property to group into sections.
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:context
sectionNameKeyPath:@"monthAsSectionName"
cacheName:@"Root"];
}
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
你的问题基本上是你试图重复使用 1 FRC 但你从未销毁和重新配置它。
将一个 FRC 保持在一个单独的实例中并尝试重新配置它并没有太多好处,它甚至不是共享状态。
最好让每个视图控制器创建和维护自己的 FRC,这样您就不必担心销毁和共享问题。内存不应该是一个问题,因为 FRC 本身是最小的,并且每个都会出错需要托管对象(一定要在你的获取请求上设置批处理大小以提高它们的效率,就像你目前拥有的那样:-))。