如何检查UICollectionView中插入和删除的项目,看到意外删除的项目 [英] How to check inserted and deleted items in UICollectionView, seeing unexpected deleted item

查看:542
本文介绍了如何检查UICollectionView中插入和删除的项目,看到意外删除的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这种特殊情况下,很难整合 UICollectionView NSFetchedResultsControllerDelegate

I am struggling to integrate UICollectionView and NSFetchedResultsControllerDelegate in this particular case.

在我的视图控制器中,我获取一个父实体,但在集合视图中显示其多个子实体。当在此视图控制器中创建一个新的父实体( insertNewObjectForEntityForName )并自动创建一个Child实体并将其添加到父实体 addChildObject ,我可以通过按一个按钮添加更多的子实体,并成功保存对象。不幸的是,由于某些原因, NSFetchedResultsControllerDelegate 方法未被调用,具体来说, controllerDidChangeContent 从未被调用,

In my view controller, I fetch a Parent entity, but display its multiple Child entities in the collection view. When creating a new Parent entity in this view controller (with insertNewObjectForEntityForName) and automatically creating a Child entity and adding it to the parent entity with addChildObject, I can add more Child entities by pressing a button, and save the object successfully. Unfortunately, for some reason the NSFetchedResultsControllerDelegate methods are not called, specifically, controllerDidChangeContent is never called and the collection view doesn't get updated.

当我获取已经存在的父实体,然后尝试通过添加新的Child对象来更改它时,应用程序崩溃,但出现以下异常:

When I fetch an already existing Parent entity, and then try to change it by adding new Child objects, the app crashes with the following exception:

*** Assertion failure in -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:], 
/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3505.17/UICollectionView.m:4211

错误:

CoreData: error: Serious application error.  An exception was caught 
from the delegate of NSFetchedResultsController during a call to  
controllerDidChangeContent:.  Invalid update: invalid number of items 
in section 0.  The number of items contained in an existing section 
after the update (5) must be equal to the number of items contained in 
that section before the update (4), plus or minus the number of items 
inserted or deleted from that section (1 inserted, 1 deleted) and plus 
or minus the number of items moved into or out of that section (0 moved 
in, 0 moved out). with userInfo (null)

最让我困惑的是,它显示有一个已删除的项目在(1插入,1删除),其实,我只是添加一个项目(初始化一个Child实体 insertNewObjectForEntityForName 并添加 [parent addChildObject:child]

What puzzles me the most, is that it shows that there is a deleted item in (1 inserted, 1 deleted), when, in fact, I am only adding an item (initializing a Child entity with insertNewObjectForEntityForName and adding it with [parent addChildObject:child].

在所有这些情况下,我不是我希望向父实体添加对象来触发 NSFetchedResultsControllerDelegate 方法。

In all these cases, I am not saving the context. I expect adding objects to the parent entity to trigger NSFetchedResultsControllerDelegate methods.

FetchResultsController设置代码: / p>

FetchResultsController setup code:

- (NSFetchedResultsController *) fetchedResultsController {
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [self emojiFetchRequest];

    _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil cacheName:nil];
    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;
}

中初始化父对象viewWillAppear

[[self fetchedResultsController] performFetch:&error];

id fetchedObject = [[[self fetchedResultsController] fetchedObjects] firstObject];

if (fetchedObject != nil) {
    self.parent = (Parent *)fetchedObject;
    self.navigationItem.title = self.parent.name;
} else {
    self.navigationItem.title = @"New parent";
    self.parent = [NSEntityDescription insertNewObjectForEntityForName:@"Parent" inManagedObjectContext:[self managedObjectContext]];
    Child *child = [NSEntityDescription insertNewObjectForEntityForName:@"Child" inManagedObjectContext:[self managedObjectContext]];
    [self.parent addFramesObject:frame];
}

添加新的子对象:

Child *child = [NSEntityDescription insertNewObjectForEntityForName:@"Child" inManagedObjectContext:[self managedObjectContext]];
[self.parent addChildrenObject:child];

[self.collectionView reloadData]; // if this isn't done, the crash happens
// but this can't animate changes


$ b b

核心数据模型:

Core Data Model:

有一种方式我可以看到这些对象?我试过记录 didChangeObject ,我只看到一个插入。我不删除任何东西。如果有任何人有任何其他想法,我很高兴听到他们。

Is there a way I can see those objects? I've tried logging didChangeObject, and I only see one insert. I am not deleting anything. If anyone has any other ideas, I'd be glad to hear them.

EDIT

如果在添加对象之后调用 [self.collectionView reloadData] ,一切似乎都能正常工作,而不会崩溃。但是,更改是动画,这是不错的,这不能用 reloadData ,我感觉有一些根本上错了我在做什么,我会

If I call [self.collectionView reloadData] after adding the objects, everything seems to work correctly without crashing. But, it would be nice for the changes to be animated, which cannot be done with reloadData, and I sense there is something fundamentally wrong with what I'm doing and I would like to fix it.

推荐答案

最佳解决方案:获取实体在获取的结果控制器中,按 parent 过滤。

Best solution: Fetch the Child entity in your fetched results controller, filter by parent.

对于插入,使用 NSFetchedResultsControllerDelegate 的纯香草实现。这将提供一个很好的动画或让你添加自己的。

For inserts, use the plain vanilla implementation of the NSFetchedResultsControllerDelegate. This will provide a nice animation or let you add your own.

这篇关于如何检查UICollectionView中插入和删除的项目,看到意外删除的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆