A simple strategy to create efficient, synchronized and highly reusable data source delegate for all UICollectionView in your application
In mobile development the Model-View-Controller architecture can quickly evolve to a Massive-View-Controller nightmare where a cumbersome view controller manages almost everything in the application.
Massive view controllers are an antipattern because they are difficult to scale, test and mantain. A trick to avoid them is to remember these principles:
Specialization: one objet, one task. Delegation: share responsability for targeted or iterative tasks Communication: when complexity increase notifications can ease Abstraction: make code as reusable as possible
Apple Cocoa API makes an extensive use of delegation for one-to-one communication between instances. Delegation is a powerful tool that should be employed at its best.
A common practice is to delegate anything to the view controller. This delegation strategy ends with the implementation of some methods (the ones required by the protocol we want to conform to) directly inside the body of the view controller.
This code is sneaky because it seems to be clean an compact... until we need to scale or add new features to the application
A better strategy is to delegate some responsibilities (like data sourcing, for exemple) to external objects instanciated in the view controller, making the code more reusable, expressive and semantically coherent.
Let's consider a mobile application with several collection views distributed on indipendent view controllers. For each of them we should implement at least two methods and instanciate an array with the data. Morover we should sychronize this array with the data contained in the collection view cells for the whole view controller lifecycle.
This was the old way! The smarter one is to create a new class conformed to UICollectionViewDataSource protocol that manages everything autonomously.
This class will be reusable for any collection view with any data type. Two callbacks are used to instanciate the model and to configure the collection view cells
After this, we just need to create a CollectionDataSourceManager instance in the view controller
For both callbacks we use some handy Swift features: type inferring and implicit closure parameters ($0, $1).
See documentation for details
The first callback is a oneliner that creates an instance of the model:
The second one assigns the model to the custom UICollectionViewCell class:
That's all. Collection view delegation is done once forever. It is fully reusable and indipendent.