apptekstudios / ASCollectionView

A SwiftUI collection view with support for custom layouts, preloading, and more.
MIT License
1.35k stars 161 forks source link

onScroll CPU issues #237

Closed trevordevs closed 2 years ago

trevordevs commented 2 years ago

onScroll modifier increase CPU usage from 30% to +90% When I am using the onScroll modifier with a switch case there is a clear CPU problem that can even cause the UI to become laggy.

To Reproduce -Single section ASCollectionView -onScroll -switch case on contentOffset.y -switch case controls 2-3 State booleans

Expected behaviour Maybe a 10-20% increase in CPU usage at most

onScroll Example

.onScroll({ contentOffset, contentSize in
   DispatchQueue.main.async {
      switch contentOffset.y {
       case -1000 ..< -50:
          self.isTryingToRefresh = true
       case -50 ..< 30:
          self.isTryingToRefresh = false
          self.shouldShowNavBar = false
       case 30 ..< .greatestFiniteMagnitude:
           self.shouldShowNavBar = true
       default:
           self.isTryingToRefresh = false
           self.shouldShowNavBar = false
      }
   }
})

Xcode Version:

Simulator, Device, Both?

trevordevs commented 2 years ago

Fixed by replacing State var with @Published ObservableObject