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

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


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
}

Replies

The text is so poorly formatted that it is hard to read.

Please reformat using the <> formatter tool.


And please change the title of the thread for a more concise one, such as 'Performance of CollectionView loading'

There are a lot of issues in your code, hard to tell which is causing most problems.

Notably, several intializations are done in the wrong func.


You should look at some code tutotial to learn how to use those functions.


Your func numberOfItemsInSection is bizarre.

Why do you initialize parameters here and deal with animation ? This has nothing to do here.


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



This func should simply be:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
   return self.productList.count
}



cellForItemAt


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
}



You do not show what setData is.

problem of slowliness may be there, we cannot guess.


In viewDidLoad


You don't stop the animation.


In

GetItems()
// Note that the func name should start with lowerCase getItems()


Why do you set delegate and register here ???

    self.collectionView.dataSource = self 
    self.collectionView.register(UINib(nibName:"ItemCell", bundle: nil),   forCellWithReuseIdentifier: "ItemCell")


That should be in viewDidLoad

You should also set the delegate for the CollectionView in viewDidLoad