NSCollectionView删除ManagedObject会导致NSValueTransformer运行问题

时间:2021-02-25 02:44:29

I have an NSCollectionView that shows a grid of prototype items, this is all handled via Core Data, Interface Bindings and an NSValueTransformer to show a placeholder image inside the NSCollectionViewItem if no real image exists.

我有一个NSCollectionView,它显示了一个原型项目的网格,这些都是通过Core Data、接口绑定和一个NSValueTransformer来处理的,如果没有真实的图像,就在NSCollectionViewItem中显示一个占位符图像。

When I want to delete an item from the NSCollectionView it removes it from the collection view but flashes up my placeholder image (triggered from my NSValueTransformer subclass) for a micro second just before it vanishes from view.

当我想从NSCollectionView中删除一个条目时,它会从collection视图中删除它,但在它从view中消失之前,它会在我的占位符映像(从NSValueTransformer子类中触发)上闪烁一微秒。

I can not find a way to stop the NSValueTransformer running when it detects that an object in the MOC has been deleted. Why is it even running?

当NSValueTransformer检测到MOC中的一个对象被删除时,我找不到停止它运行的方法。为什么它还在运行?

The steps I take are:

我采取的步骤是:

for (MyEntityClass * obj in [myArrayController selectedObjects]) {

    [myArrayController removeObject: obj];    // This on its own works fine.
    [managedObjectContext deleteObject: obj]; // This causes a flash

}

If I just delete the object from the MOC (without removing from the arrayController) it flashes the placeholder image for a micro second.

如果我从MOC中删除对象(而不从arrayController中删除),它会在一微秒内闪烁占位符图像。

Is there a way to stop the deletion of a MOC object from causing the CollectionView to run the NSValueTransformer before removing it from the view?

是否有一种方法可以阻止MOC对象的删除,使其在将NSValueTransformer从视图中删除之前导致其运行?

I tried adding a delay between ArrayController removeObject and the MOC deleteObject which fixes the flashing of the icon but then causes a Core Data field can not be nil errors if the user deletes a group of items in one go.

我尝试在ArrayController removeObject和MOC deleteObject之间添加一个延迟,它修复了图标的闪烁,但是如果用户一次删除了一组条目,则导致核心数据字段不能为nil。

Any ideas to stop the placeholder image flashing up just before it removes the object from the collection view?

有什么办法可以在占位符图像从集合视图中移除对象之前阻止它闪烁?

1 个解决方案

#1


0  

Solved it.

解决它。

The problem was due to saving the managedObjectContext inside the for loop (not shown in sample code above). When the MOC saves it was refreshing the collectionView while it was still removing the object due to the animation which caused the glitch.

问题是由于在for循环中保存managedObjectContext(上面的示例代码中没有显示)。当MOC保存时,它正在刷新集合视图,而由于动画引起的故障,它仍在删除对象。

#1


0  

Solved it.

解决它。

The problem was due to saving the managedObjectContext inside the for loop (not shown in sample code above). When the MOC saves it was refreshing the collectionView while it was still removing the object due to the animation which caused the glitch.

问题是由于在for循环中保存managedObjectContext(上面的示例代码中没有显示)。当MOC保存时,它正在刷新集合视图,而由于动画引起的故障,它仍在删除对象。