UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Diffable Data Source: Mixing static and dynamic sections with fetched results controller?

Forums > iOS

Hi all!

I am working on a somewhat complex screen which is backed by UICollectionView and I wondering if it is possible (and a good idea) to mix static sections with dynamic sections that are driven by NSFetchedResultsController. Basically my two first sections are static and both have just one cell.

The rest of the sections is dynamic. Here is my "pro" illustration:

Diffable question

The "S" denotes static sections and "DB" sections that display Core Data entities.

I have worked with Diffable in the past, but I had only single section. How would I go about creating snapshot for this layout? Ideally with NSFetchedResultsController so I dont have to do manual refreshes when content changes.

Thanks a ton.

3      

Are you bound to UIKit or are you able to use SwiftUI for this screen? Because with SwiftUI (and CoreData) it would be a none issue.

3      

I am theoretically able to use SwiftUI but I dont want to.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Then I would create a new view, rebuild the design for the two static sections and one Collection View controlled by the NSFetchedResultsController.

3      

It turned out to be a quite easy task without the NSFetchedResultsController. I realized that diffable datasource and its snapshot can be composed of anything I want. So I have one enum for sections and another enum for different item types. So I am wrapping the entities into case with associated value and then it is just a matter of constructing the snapshot. So far seems to work great.

4      

Thank you for sharing your experience. This is really useful.

3      

Could you post your code, very curious about it! Good learning stuff!

3      

I think once I am a bit more comfortable with it, I can post simplified example or write a short post (so I have reference guide in the future :D)

4      

Great looking forward to that! And really appreciate your contributions to this community!

3      

So in the end I decided first to get more experience with compositional collection views and created this public repo with my experiments - https://github.com/nemecek-filip/CompositionalDiffablePlayground.ios

It currently has three screens - one more complex and then two simple - list & grid. Feel free to check it out.

Important note: I am still just learning this API so don't take anything there as a best practice :D

5      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.