Swift UICollectionView

I created shopping cart using UICollectionView. At first of loading, speed is good. But when go to another page and go back to UICollectionView page, loading consume more time. All time, program will call to Rest API and bind to the UICollectionView

What's the question? Could you elaborate on it further?

I used UICollectionView for list product item. when it load at first time is working good. But after that it will take more time. how can reduced the loading time.

My code as below


import Foundation
import UIKit
import Foundation

class Order: UIViewController {

let url="http://apiURL"

@IBOutlet weak var titleView: UIView!
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var lblUser: UILabel!
@IBOutlet weak var lblPhone: UILabel!
@IBOutlet weak var leftMenuView: UIView!
@IBOutlet weak var landingConstaint: NSLayoutConstraint!
@IBOutlet weak var loadingView: UIView!
@IBOutlet weak var loadingActivity: UIActivityIndicatorView!
@IBOutlet weak var bannerImage: UIImageView!

var productList: Array<Product> = Array()
var menuShow=false
var customerId=0
var isNotCheck=true
var isCheck=true

override func viewDidLoad() {
    super.viewDidLoad()
    self.HideKeyboard()
    self.loadingActivity.isHidden = false
    self.loadingView.isHidden = false
    self.loadingActivity.startAnimating()

    let userDefaults = UserDefaults.standard
    self.customerId  = userDefaults.integer(forKey: "CustomerId")
    self.isCheck=true


    if(self.isNotCheck){
        self.isNotCheck=false
        collectionView.clearsContextBeforeDrawing=true
        collectionView.dataSource=nil

        if !userDefaults.bool(forKey: "ProductDownloaded") {
            GetItems()
            userDefaults.set( true, forKey: "ProductDownloaded")
        }
        else{
            GetItems()
        }
    }

    UIView.animate(withDuration: 0.3, animations: {
        self.view.layoutIfNeeded()
    })
}


func GetItems(){
    let productService=ProductService(url: url)

    productService.GetProducts(CustomerId: String(self.customerId))
    {
        (Products) in
        self.productList = Products
        self.collectionView.dataSource = self
        self.collectionView.register(UINib(nibName:"ItemCell", bundle: nil),   forCellWithReuseIdentifier: "ItemCell")

        if(self.isCheck){
            self.isCheck = false             
            self.leftMenuView.layer.shadowOpacity=1
            self.leftMenuView.layer.shadowRadius=6
        }
        return
    }
}

}

extension Order : UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
self.loadingActivity.isHidden = true
self.loadingView.isHidden = true
self.loadingActivity.stopAnimating()
return self.productList.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ItemCell" , for: indexPath) as! ItemCell
    cell.setData(product:self.productList[indexPath.row])
    return cell
}

}

Sorry for the late reply.

This forums isn’t apt for for questions regarding UIKit though. I’d suggest you go to Stack Overflow or Apple Dev Forum for that.

My best guess is that the ProductService.GetProduct is causing it.
I’d suggest that you use Instrument to benchmark and find cpu spikes when you reload the page.

1 Like