NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Where am I going wrong?

Forums > Swift

Hi,

So I am calling this into a colleciton view. This example is for SwiftUI but I am creationg a collection view with a list layout.

Example: https://hygraph.com/blog/swift-with-hygraph

I have done all the other setup which you can find in the gist but I keep getting these errors: [Common] Snapshot request 0x60000348db30 complete with error: <NSError: 0x600003489020; domain: FBSSceneSnapshotErrorDomain; code: 4; reason: "an unrelated condition or state was not satisfied">

My Gist: https://github.com/ImranRazak1/HygraphSwift

View Controller

import UIKit

class ViewController: UIViewController {

    enum Section {
        case main
    }

    var collectionView: UICollectionView!
    var dataSource: UICollectionViewDiffableDataSource<Section,Product>?
    var products: [Product]  = []

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        navigationItem.title = "Products"
        view.backgroundColor = .white

        //Uncomment when needed
        configureHierarchy()
        configureDataSource()
        collectionView.register(ListCell.self, forCellWithReuseIdentifier: "ListCell")

    }

    func loadProducts() async {
            self.products = await APIService().listProducts()
        }

    func configure<T: SelfConfiguringCell>(with product: Product, for indexPath: IndexPath) -> T {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ListCell", for: indexPath) as? T else {
            fatalError("Unable to dequeue Cell")
        }
        cell.configure(with: product)
        return cell
    }

}

extension ViewController {
    private func createLayout() -> UICollectionViewLayout {
        let config = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
        return UICollectionViewCompositionalLayout.list(using: config)
    }
}

extension ViewController {
    private func configureHierarchy() {
        collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: createLayout())
        collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.addSubview(collectionView)
        collectionView.delegate = self
    }

    private func configureDataSource() {

        let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, Product> { (cell, indexPath, product) in
            var content = cell.defaultContentConfiguration()
            content.text = "\(product.name)"
            cell.contentConfiguration = content
        }

        dataSource = UICollectionViewDiffableDataSource<Section, Product>(collectionView: collectionView) {
            (collectionView: UICollectionView, indexPath: IndexPath, identifier: Product) -> UICollectionViewCell? in
            return collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: identifier)
        }

        //inital data

        var snapshot = NSDiffableDataSourceSnapshot<Section, Product>()
        snapshot.appendSections([.main])
        //Problem loading this information
        snapshot.appendItems(products, toSection: .main)
        dataSource?.apply(snapshot, animatingDifferences: false)

        Task {
            do {
                await self.loadProducts()
               snapshot.appendItems(products, toSection: .main)
                await dataSource?.apply(snapshot, animatingDifferences: false)
            }
        }

    }

}

extension ViewController: UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
    }
}

Best, Imran

   

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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.