HomeiOS Developmentios - Stopping A number of Calls to loadProducts Perform

ios – Stopping A number of Calls to loadProducts Perform


I’ve a ProductListScreen, which shows all merchandise. I wish to ensure that a person can not carry out a number of concurrent calls to the loadProducts. At present, I’m utilizing the next code and it really works. However I’m in search of higher choices and perhaps even transferring the logic of job cancellation contained in the Retailer.

struct ProductListScreen: View {
    
    let class: Class
    @Atmosphere(Retailer.self) personal var retailer
    @Atmosphere(.dismiss) personal var dismiss
    @State personal var showAddProductScreen: Bool = false
    @State personal var isLoading: Bool = false
    
    personal func loadProducts() async {
                
        guard !isLoading else { return }
        isLoading = true
        
        defer { isLoading = false }
                
        do {
            attempt await retailer.loadProductsBy(categoryId: class.id)
        } catch {
            // present error in toast message
            print("Didn't load: (error.localizedDescription)")
        }
    }
    
    var physique: some View {
        ZStack {
            if retailer.merchandise.isEmpty {
                ContentUnavailableView("No merchandise accessible", systemImage: "shippingbox")
            } else {
                Listing(retailer.merchandise) { product in
                    NavigationLink {
                        ProductDetailScreen(product: product)
                    } label: {
                        ProductCellView(product: product)
                    }
                }.refreshable(motion: {
                    await loadProducts()
                })
            }
        }.overlay(alignment: .heart, content material: {
            if isLoading {
                ProgressView("Loading...")
            }
        })
        .job {
            await loadProducts()
        }

Right here is my implementation of Retailer.

@MainActor
@Observable
class Retailer {
    
    var classes: [Category] = []
    var merchandise: [Product] = []
    
    let httpClient: HTTPClient
    
    init(httpClient: HTTPClient) {
        self.httpClient = httpClient
    }
   
    
    func loadProductsBy(categoryId: Int) async throws {
        
        let useful resource = Useful resource(endpoint: .productsByCategory(categoryId), modelType: [Product].self)
        merchandise = attempt await httpClient.load(useful resource)
    }

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments