NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

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.

   

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.

   

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

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED Building and maintaining in-app subscription infrastructure is hard. Luckily there's a better way. With RevenueCat, you can implement subscriptions for your app in hours, not months, so you can get back to building your app.

Try it for free

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.

   

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.

1      

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

   

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

   

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)

1      

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

   

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

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Building and maintaining in-app subscription infrastructure is hard. Luckily there's a better way. With RevenueCat, you can implement subscriptions for your app in hours, not months, so you can get back to building your app.

Try it for free

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

Reply to this topic…

You need to create an account or log in to reply.

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.