I am fetching some data from firebase and showing it to the UITableView
but each time when I scroll the tableview it freezes for 2-3 second.Here is the UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
list.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIndetifire) as? HallListCell else {
return UITableViewCell()
}
cell.configure(list: list[indexPath.row])
return cell
}
and here is the loadData function which observe and fetch data from firebase and reload the UITableView
func loadData(){
guard let uid = currentUID else {
return
}
list.removeAll()
self.ref.child("users/\(uid)").child("SWITCH/HALL").observe(.childAdded, with: { (snapshot) -> Void in
guard let value = (snapshot.value) as? [String : String] else {
return
}
guard let name = value["name"],let img = value["img"], let status = value["status"] else {
return
}
self.list.append(DataModel(name: name, img: img, status: status,keyPrimices: "HALL",keyAppliances: snapshot.key))
DispatchQueue.main.async {
self.tblListOfHallAppliances.reloadData()
self.tblListOfHallAppliances.isHidden = false
self.activityIndicator.isHidden = true
}
})
}
code in cell.configure(list: list[indexPath.row]) is written below which is placed in cell
func configure(list: DataModel) {
keyPrimices = list.keyPrimices
keyAppliances = list.keyAppliances
lblAppliancesName.text = list.name
switchStauts.isOn = (list.status == "ON")
switch keyAppliances {
case "FAN":
list.status == "ON" ? imgIcon.fanAnimation() : imgIcon.fanAnimationStop()
case "LIGHT":
list.status == "ON" ? imgIcon.lightAnimation() : imgIcon.lightAnimationStop()
case "AC":
list.status == "ON" ? imgIcon.acAnimation(): imgIcon.acAnimationStop()
case "TV":
list.status == "ON" ? imgIcon.tvAnimation(): imgIcon.tvAnimationStop()
default:
if let url = URL(string: list.img) {
loadImage(url: url, imageOutlet: imgIcon)
}
}
}
UIImageView
extension is below
extension UIImageView {
func fanAnimation() {
let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotation.toValue = NSNumber(value: Double.pi * 2)
rotation.duration = 0.5
rotation.repeatCount = Float.greatestFiniteMagnitude
self.image = UIImage(named: "fan")
self.layer.add(rotation, forKey: "rotationAnimation")
}
func fanAnimationStop() {
self.layer.removeAllAnimations()
self.image = UIImage(named: "fan")
}
func lightAnimation() {
let animation = CABasicAnimation(keyPath: #keyPath(CALayer.opacity))
animation.fromValue = 0.2
animation.toValue = 1.0
animation.duration = 0.5
animation.autoreverses = true
animation.repeatCount = Float.greatestFiniteMagnitude
self.image = UIImage(named: "light_on")
self.layer.add(animation, forKey: "fade")
}
func lightAnimationStop() {
self.layer.removeAllAnimations()
self.image = UIImage(named: "light_off")
}
func acAnimation () {
let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotation.toValue = NSNumber(value: Double.pi/9)
rotation.fromValue = NSNumber(value: -Double.pi/9)
rotation.duration = 0.5
rotation.autoreverses = true
rotation.repeatCount = Float.greatestFiniteMagnitude
self.image = UIImage(named: "ac_on")
self.layer.add(rotation, forKey: "acAnimation")
}
func acAnimationStop() {
self.layer.removeAllAnimations()
self.image = UIImage(named: "ac_off")
}
func tvAnimation () {
let rotation = CABasicAnimation(keyPath: "transform.scale")
rotation.toValue = NSNumber(value: 1)
rotation.fromValue = NSNumber(value: 0.5)
rotation.duration = 0.5
rotation.autoreverses = true
rotation.repeatCount = Float.greatestFiniteMagnitude
self.image = UIImage(named: "tv_on")
self.layer.add(rotation, forKey: "tvAnimation")
}
func tvAnimationStop() {
self.layer.removeAllAnimations()
self.image = UIImage(named: "tv_off")
}
}