Swift 3 中的 UICollectionView + NSFetchedResultsController

UICollectionView + NSFetchedResultsController in Swift 3

在我当前的项目中,我将 NSFetchedResultsControllerUICollectionView 集成在一起,效果很好。目前我尝试将项目升级到 Swift 3 和 Xcode 8,这会导致以下错误消息

This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread.

我正在使用 BlockOperation 数组对 UICollectionView 的更改进行排队。这是我如何插入新项目的示例。

self.blockOperations.append(BlockOperation(block: {
   collectionView?.insertItems( at: [newIndexPath!])
}))

这是我目前对 controllerDidChangeContent

的实现
var blockOperations = [BlockOperation]()

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    if self.shouldReloadCollectionView {
        self.collectionView.reloadData()
    }
    self.collectionView?.performBatchUpdates({    
        for operation in self.blockOperations {
            OperationQueue.current?.addOperation(operation)
        }
        }, completion: { (completed) in
            print(completed)
    })
}

有没有人在 Swift 3 中用 UICollectionView 实现了 NSFetchedResultsController 并且可以帮我解决这个问题?

谢谢 Pawel,您的 Gist 很有帮助。我分叉并更新了它以完全支持 Swift 3iOS10。因为它没有解决从后台线程更新 UICollectionView 的问题。

错误是由后台线程更新 UICollectionView 引起的。

This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread.

要使 NSFetchedResultsControllerUICollectionView 的组合正常工作,您必须将 collectionView 对象的每个更新包装在

DispatchQueue.main.async {
    // i.e. insertion of new items                          
    this.collectionView!.insertItems(at: [newIndexPath!])
}

这里是link完整更新Gist