Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSFetchedResultsController在推送后保存objectAtIndex时崩溃_Ios_Iphone_Objective C_Nsfetchedresultscontroller - Fatal编程技术网

Ios NSFetchedResultsController在推送后保存objectAtIndex时崩溃

Ios NSFetchedResultsController在推送后保存objectAtIndex时崩溃,ios,iphone,objective-c,nsfetchedresultscontroller,Ios,Iphone,Objective C,Nsfetchedresultscontroller,我的应用程序中有两个UIViewController(与此问题相关) 它们中的每一个都有一个NSFetchedResultsController,其中一个请求位于名为News的同一实体上。它们也有相同的排序描述符,并且使用相同的UITableViewCell子类来显示所有内容。它们还都符合NSFetchedResultsControllerDelegate,并运行委托方法 唯一的区别是第一个视图控制器,我称之为SummaryVC,只显示第一个(最多)获取的6个对象。而另一个,NewsFeedVC

我的应用程序中有两个
UIViewController
(与此问题相关)

它们中的每一个都有一个
NSFetchedResultsController
,其中一个请求位于名为
News
的同一
实体上。它们也有相同的排序描述符,并且使用相同的
UITableViewCell
子类来显示所有内容。它们还都符合NSFetchedResultsControllerDelegate,并运行委托方法

唯一的区别是第一个视图控制器,我称之为
SummaryVC
,只显示第一个(最多)获取的6个对象。而另一个,
NewsFeedVC
显示了所有的对象,它还页面显示了更多对象的下载。因此,
SummaryVC
中的委托方法只运行
[self.tableView reloadData]

首次启动应用程序时,将显示
SummaryVC
,并触发下载前6个
News
对象(从JSON转换而来),并将其保存在BG线程中

FRC
然后选择保存并显示实体

但是…

在应用程序的不同部分之间按下并弹出后,会时断时续地(我讨厌这个词)。我回到
SummaryVC
,应用程序将崩溃

它也总是一样的崩溃

CoreData: error: Serious application error.  Exception was caught during Core Data change processing.  This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.  *** -[_PFArray objectAtIndex:]: index (46) beyond bounds (6) with userInfo (null)
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[_PFArray objectAtIndex:]: index (46) beyond bounds (6)'
在这种情况下,已经有超过6个实体加载到核心数据中。6使我怀疑属于
SummaryVC
FRC

我试过几种方法来解决这个问题

  • 视图上的
    FRC
    委托设置为nil将消失
  • 视图上的
    FRC
    设置为nil将消失
  • 当委托方法调用时,检查
    VC
    实际上是屏幕上的
    VC
  • NewsFeedVC
    视图将消失
    中,它现在取消其队列中的所有下载操作
  • 问题似乎是由
    FRC
    拾取
    saveContext
    导致的。i、 e.如果我进入
    NewsFeedVC
    并触发下载,但在下载完成之前弹出VC,那么这似乎会触发崩溃

    现在,代码方面。我自己的代码永远不会导致崩溃

    我可以显示
    FRC
    设置

    - (NSFetchedResultsController *)fetchedResultsController
    {
        if (!_fetchedResultsController) {
            NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"News"];
    
            NSSortDescriptor *dateSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO];
            NSSortDescriptor *sourceSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"source" ascending:YES];
            NSSortDescriptor *titleSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
            fetchRequest.sortDescriptors = @[dateSortDescriptor, sourceSortDescriptor, titleSortDescriptor];
    
            _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.moc sectionNameKeyPath:nil cacheName:@"AllNewsItems"];
            _fetchedResultsController.delegate = self;
    
            NSError *error = nil;
            [_fetchedResultsController performFetch:&error];
    
            if (error) {
                NSLog(@"Error performing fetch: %@, %@", error, [error userInfo]);
            }
        }
        return _fetchedResultsController;
    }
    
    我在想也许我应该把FRC从一个物体传递到另一个物体。也就是说,将其注入到一个属性中,以便只有一个属性。这样做合法吗

    编辑

    这可能与对哥特人获取的结果控制器使用相同的缓存名称有关吗

    EDIT2

    不,如果我更改缓存名称,它仍然会崩溃

    EDIT3


    好的,我每次都可以复制这个。如果我启动
    NewsFeedVC
    滚动,然后在滚动过程中,我弹出到
    SummaryVC

    感谢Martin R的修复

    我从FetchedResultsController的初始化中删除了
    cacheName
    ,它解决了这个问题


    谢谢。

    您是否尝试过
    cacheName:nil
    ?(我认为如果没有分区,使用缓存没有任何好处。)我会尝试一下,并让您知道。Thanks@MartinR繁荣我想已经做到了!我再也不能复制它了。速记。将缓存名设置为nil或只是在两个视图之间传递FRC可以修复此问题。不过我更喜欢缓存名称修复。谢谢