UICollectionView + NSFetchedResultsController in Swift 3
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.
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.
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!])
}
Here is a link to the full updated Gist
NSFetchedResultsContollerDelegate for CollectionView
Combining a fetched results controller with a collection view is a bit tricky.
The problem is explained in
- http://ashfurrow.com/blog/how-to-use-nsfetchedresultscontroller-with-uicollectionview
If you're looking for how to get around the
NSInternalInconsistencyException
runtime exception with
UICollectionView
, I have an example on GitHub detailing how to queue
updates from the NSFetchedResultsControllerDelegate.The problem is that the existing
UITableView
class usesbeginUpdates
andendUpdates
to submit batches to the table view.UICollectionView
has a newperformBatchUpdates:
method, which takes a block parameter
to update the collection view. That's sexy, but it doesn't work well
with the existing paradigm for NSFetchedResultsController.
Fortunately, that article also provides a sample implementation:
- https://github.com/AshFurrow/UICollectionView-NSFetchedResultsController
From the README:
This is an example of how to use the new
UICollectionView
with
NSFetchedResultsController
. The trick is to queue the updates made
through theNSFetchedResultsControllerDelegate
until the controller
finishes its updates.UICollectionView
doesn't have the same
beginUpdates
andendUpdates
thatUITableView
has to let it work easily
withNSFetchedResultsController
, so you have to queue them or you get
internal consistency runtime exceptions.
Using UICollectionView with CoreData and NSFetchedResultsController
Sounds like an issue I had a while ago, when I was using objective C.
Check out
UICollectionView Assertion failure
I had to do a work around to get my FRC to work with the collection view.
Seems like you found the same git as me, here is the fix that solves the collection view issue.
https://github.com/AshFurrow/UICollectionView-NSFetchedResultsController/issues/13
Reorder cells in UICollectionView + NSFetchedResultsController
As you've found, you can't just tell NSFetchedResultsController
to change the order of objects in its fetchedObjects
array. That array is sorted based on your sort descriptor, and is read-only.
So you have two possible options:
- Change the property you're using in the sort descriptor, so that the new sort result will match the result of the drag and drop operation. Save changes, and
NSFetchedResultsController
delegate methods will fire. You've changed the order and your fetched results controller will reflect that in itsfetchedObjects
array. - Don't use
NSFetchedResultsController
, just do a fetch and save the results in your own array. Re-order the items in your own array but don't change anything that Core Data cares about.
UICollectionView Cells Handled by NSFetchedResultsController not Repopulating Images
I experienced an issue in the NSFetchedResultsController behaviour. If NSPredicate is set on its fetch request, the delegate methods are not called.
Try to remove the predicate to see if you have this problem too.
I'm trying to see if it's a bug in Xcode, or some unhanded error related to faults, or if the new query token system is used in the NSFetchedResultsController class and we need to do something about it.
NSFetchedResultsController per cell in a collectionView
Since we require separate queries in each cell, we ended up setting separate NSFetchedResultsController
in each cell when it's dequeued. And then have the NSFetchedResultsController
set as nil when prepareForReuse
.
Related Topics
Convert Data to Dispatchdata in Swift 4
How to Remove The Fading Animation on .Ondelete Swiftui
Calling Asynchronous Method Inside For-Loop
Watchkit Extension Cannot Read from Icloud
Moving from Nsurlconnection to Nsurlsession for Soap Post in Swift
Showing Action Sheet in The Custom Cell in Swift
Swift: Right/Left Click on Nsstatusbar
Swiftui Index Out of Range in Foreach
How to Download Video Urlstring from Firebase Database Not Storage in Swift
How to Collect The Return Value of a Function (Swift 3)
How to Have a Searchbar Which Shows Suggestions with Different UItableview
Nsurlsession Upload File to Server Swift
Swift, Detect Ibeacons on The Background and Send Notifications When in Range
Will a a Weak Wrapper Class Throw Off a Hashable Based Collection When The Wrapped Value Is Set Nil