ios – How to call Async Task inside view did load for Collection View?

0
114

[ad_1]

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

[ad_2]