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

CollectionView Diffable DataSource question

Forums > iOS

Just a quick question about diffable datasources (collectionview). There's no need to have an array of your type anymore, right? Or wrong?

Why I am asking; I got this collectionview of Projects. When I preload some dummy projects, I see them and I can edit them. When I add a new Project (snapshot 1 new Project), after adding/editing I see only the newly added/edited project, the rest is gone. If I start empty and add a new Project, I see the new Project in the collectionview. If I add another Project, I just see this lastly added Project.

What Am I missing here, I think I am missing something fundamental ...

3      

This is a pretty good tutorial on how to use diffable data source. I believe if you're trying to append a new project to your data source while animating the difference, you'll still need to maintain an array of previous projects.

https://wwdcbysundell.com/2019/diffable-data-sources-first-look/

4      

Yes, thank you. It finally occured to me that the snapshot you provide is all the collectionview will show. So I have to maintain state by first capturing the current state of the datasource and then apply the new snapshot, including existing and new projects ...

I think I can get a bit further now ....

3      

Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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

Ok, I've got a preliminary working version now for adding and updating Projects. But I wonder if all the array stuff I do is the way to go. I feel I should do more with the snapshot. Should all logic from the save method be in the update method?

ProjectsViewController: UIViewController {
(...)
    //DataSource methods (snapshot)
    func updateData(on projects: [Project]) {
        var snapshot = NSDiffableDataSourceSnapshot<Section, Project>()
        snapshot.appendSections([Section.normal])
        snapshot.appendItems(projects)

        //apply() is safe to call from a background queue!
        self.dataSource.apply(snapshot, animatingDifferences: true)
    }

(...)
}

//MARK: - ProjectHandler (delegation method)
extension ProjectsViewController: ProjectHandler {
    func save(_ project: Project, withImage image: UIImage?) {
        //Make sure LastEdited Date gets updated
        var projectToBeSaved = project
        projectToBeSaved.lastEdited = Date()

        //Current state
        var projects = dataSource.snapshot().itemIdentifiers

        //Replace the changed project
        if projects.contains(projectToBeSaved) {
            let index = projects.firstIndex(of: projectToBeSaved)
            projects.remove(at: index!)
            projects.append(projectToBeSaved)
        //Add the new projects
        } else {
            projects.append(projectToBeSaved)
        }

        //sort on lastly edited
        projects.sort { $0.lastEdited > $1.lastEdited }

        //TODO: - Write to json

        //update the snapshot
        updateData(on: projects)
        collectionView.reloadData()
    }
}

There is very little on the web as of current on adding and deleting with diffable datasources. Any help appreciated!

3      

@Gakkienl I would leave update method as it is to be honest. Just a small remark, you do not need to call reloadData after applying a snapshot, it should reload on its own.

4      

@artrmz Thanks, will do (for now). I'll keep looking out for content on the web on this matter.

EDIT The .reloadData() really is needed, otherwise I still see the old version after updating a project (tested)

3      

@Gakkienl There must be an issue somewhere else. I am pretty sure it's not needed. Are you sure it uses your collection's dataSource is set to your UICollectionViewDiffableDataSource? I have also wrote an article on my blog about this if you look for more information, here is a playground for this: https://gist.github.com/artrmz/8a12012040084045aa4980ff9aab4ad0 and as you can see relloadData wasn't needed here.

Let me know if you got any more questions :)

4      

I'm 100% sure the datasource is used. But after updating an item I do need to do a reload of the tableview, or it will still show the unchanged data ...

Thanks for the gist! Looking at it now!

EDIT Perhaps when I've implemented a few more things you could perhaps review my code some time ....

3      

Sure, feel free to poke me here on forum or DM me on Twitter and I can take a look.

4      

Thanks, might be really helpful to me. Give me a few days ... And really appreciate your efforts!

3      

Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.