markiv / SwiftUI-Shimmer

Shimmer is a super-light modifier that adds a shimmering effect to any SwiftUI View, for example, to show that an operation is in progress. It works well on light and dark modes, and across iOS, macOS, tvOS, watchOS and visionOS.
MIT License
1.04k stars 60 forks source link

High CPU load even if hidden #3

Open denis-obukhov opened 2 years ago

denis-obukhov commented 2 years ago

Hi. If I have a view like this:

    @ViewBuilder var listView: some View {
        if isLoading {
            skeletonView.shimmer()
        } else {
            listView
        }
    }

Then even if isLoading is false is still consumes CPU. The more cells the more consumption. I have about 36% on my iPhone 11 even if there's no shimmering view on screen. If remove shimmering then CPU load is 0%.

MojtabaHs commented 2 years ago

The library uses branching for active and not active states. So first of all, you need to explicitly pass in the active argument:

.shimmering(active: shouldShim)

Also, you are using branching too! So the code will not call the false state if you just put it in the way you write it!

So you need to extract the shimmer out like:

func listView(shouldShim: Bool) -> some View {

        var listView: some View {
            if shouldShim {
                return skeletonView
            } else {
                return listView // <- What is this by the way ? do you mean self.listView ? 🤔
            }
        }

        return listView
            .shimmering(active: shouldShim)
}

This is just a work around and I hope the library supports the branching and auto animation stop for reduce the CPU usage