Swift 3 中的 UICollectionView + NSFetchedResultsController [英] UICollectionView + NSFetchedResultsController in Swift 3

查看:48
本文介绍了Swift 3 中的 UICollectionView + NSFetchedResultsController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我当前的项目中,我将一个 NSFetchedResultsController 与一个运行良好的 UICollectionView 集成在一起.目前我尝试将项目升级到 Swift 3 和 Xcode 8,这会导致以下错误消息

In my current project I integrated a NSFetchedResultsController with a UICollectionView which works fine. Currently I try to upgrade the project to Swift 3 and Xcode 8 which causes the following error message

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

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

I'm using a BlockOperation array to queue up the changes to the UICollectionView. Here is an example of how I insert a new item.

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 并且可以帮我解决这个问题?

Has anyone implemented NSFetchedResultsController with a UICollectionView in Swift 3 an can help me with this?

推荐答案

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

Thank you Pawel, your Gist was very helpful. I forked it and updated it to fully support Swift 3 and iOS10. Since it doesn't solved the problem with updating the UICollectionView from a background thread.

该错误是由从后台线程更新 UICollectionView 引起的.

The error is caused by updating the UICollectionView from a background thread.

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

要获得 NSFetchedResultsControllerUICollectionView 的组合,您必须将 collectionView 对象的每次更新包装在

To get a combination of NSFetchedResultsController and UICollectionView working you have to wrap every update of the collectionView object inside

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

这里是完整更新的要点

这篇关于Swift 3 中的 UICollectionView + NSFetchedResultsController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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