Giter Site home page Giter Site logo

happn-app / collectionandtableviewupdateconveniences Goto Github PK

View Code? Open in Web Editor NEW
15.0 3.0 2.0 31 KB

A group of conveniences for UITableView and UICollectionView to allow easier updates in coordination with NSFetchedResultsController

License: Apache License 2.0

Swift 100.00%
coredata ios nsfetchedresultscontroller uitableview uicollectionview

collectionandtableviewupdateconveniences's Introduction

CollectionAndTableViewUpdateConveniences

Platforms SPM Compatible License happn

Easily handle NSFetchedResultsController updates in a UITableView or a UICollectionView.

What’s this for?

CoreData has a wonderful tool called NSFetchedResultsController. This tool allows receiving notifications when an object matching a specific set of requirements (NSPredicate) is created or deleted in an NSManagedObjectContext. Whenever such objects are created or deleted, you can tell a corresponding UITableView or UICollectionView to insert/delete rows accordingly.

For a UITableView, it is relatively easy to do. Apple does provide a CoreData Master/Detail project template which has all of the code to do it too.

With a UICollectionView, it is a bit more tricky (because grouped updates are done via a handler instead of using the beginUpdates/endUpdates methods).

This project makes it easy for a UICollectionView (and easier for a UITableView) to use the NSFetchedResultsController class in a CoreData project: receive the notifications from the results controller and just forward them to your table or collection view directly.

Example of use

/* *** Collection View Setup *** */

/* First, how to handle a reload of a cell? */
collectionView.fetchedResultsControllerReloadMode = .handler({ [weak self] cell, object, collectionViewIndexPath, dataSourceIndexPath in
   self?.setup(cell: cell as! MyCellType, with: object as! MyObjectType)
})
/* Next, when a cell moves, what to do? Here, we do a move (other solution
 * is to delete/insert the cell).
 * When a cell is moved, it must be reloaded too. We say we want the
 * standard reload mode (the one we have defined above). */
collectionView.fetchedResultsControllerMoveMode = .move(reloadMode: .standard)



/* *** Handling the NSFetchedResultsController notifications *** */

/* We simply forward the methods to the collection view */
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
   collectionView.fetchedResultsControllerWillChangeContent()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
   collectionView.fetchedResultsControllerDidChange(section: sectionInfo, atIndex: sectionIndex, forChangeType: type)
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
   collectionView.fetchedResultsControllerDidChange(object: anObject, atIndexPath: indexPath, forChangeType: type, newIndexPath: newIndexPath)
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
   collectionView.fetchedResultsControllerDidChangeContent(endUpdatesHandler: nil)
}

Installation

The project is SPM-compatible.

You can also simply copy the two UICollectionView extensions and the UITableView extension in your project.

Credits

This project was originally created by François Lamboley while working at happn.

collectionandtableviewupdateconveniences's People

Contributors

frizlab avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

carabina frizlab

collectionandtableviewupdateconveniences's Issues

Pod

Great library! Almost feel like making it into a pod...!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.