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
}