gpt4 book ai didi

ios - NSFetchedResultsController 不会根据请求重新加载数据

转载 作者:行者123 更新时间:2023-11-29 02:13:10 26 4
gpt4 key购买 nike

希望有人能向我解释为什么我的 NSFetchedResultsController 只获取一次数据。我创建了一个带有 NSFetchedResultsController 方法的单例。我的应用程序有一个 UITabBarController,其中一个选项卡中有一个 UINavigationController。在这个 UINavigationController 中,我设置了一个带有 3 个表格 View 的 UIScrollView,全部来自 1 个 UITableViewController。它们的不同之处在于一个显示按天分组的数据,一个按周分组,一个按月分组。

我的问题是,在为 ScrollView 初始化三个 View 后,我的 NSFetchedResultController 没有为每个 View 获取数据。它只触发一次然后停止工作。在 View 之间滑动时,我在 tableview 上重新加载数据,但没有任何结果。

我注意到当我重新加载时,这段代码被多次调用。

if (_fetchedResultsController != nil)
{
return _fetchedResultsController;
}

我尝试在没有此代码的情况下运行代码,但随后我的 _fetchedResultsController 将返回 NULL。任何想法如何让这个工作?

我的代码:

日期 ListView

// 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 保持为单例并尝试重新配置它并没有太多优点,它甚至不是真正的共享状态。

最好让每个 View Controller 创建并维护自己的 FRC,这样您就无需担心破坏和共享问题。内存不应该成为问题,因为 FRC 本身很小,并且每个都会错误地指出需要托管对象(请确保在获取请求上设置批处理大小以使其高效,就像您当前所拥有的那样:-))。

关于ios - NSFetchedResultsController 不会根据请求重新加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29061053/

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