Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

High CPU usage #10

Closed
Schlabbi opened this issue Nov 24, 2020 · 14 comments
Closed

High CPU usage #10

Schlabbi opened this issue Nov 24, 2020 · 14 comments
Assignees
Labels
bug Something isn't working confirmed critical Something is very wrong here

Comments

@Schlabbi
Copy link

When using the search bar I get a high CPU usage (100% usage consistently), even when the user has not interacted with the search bar or the app at all. I only tested this on iOS 14.2.1.

Here is an example of how the high CPU usage can be reproduced quite easily:

import SwiftUI
import SwiftlySearch

class SearchViewModel: ObservableObject {
    @Published var searchText = ""
}

struct ContentView: View {
    
    @ObservedObject var viewModel = SearchViewModel()
    
    var body: some View {
        NavigationView {
            List {
                Text("foo")
            }.navigationBarSearch($viewModel.searchText)
        }
    }
}

As soon as the .navigationBarSearch is commented out, the high CPU usage disappears.

Am I doing something wrong here or could this be a bug?

@thislooksfun
Copy link
Owner

I won't be able to test this until tomorrow, but when I do it would speed up testing if I know what device(s) you have tested this on (phone, iPad, simulator, physical, etc.).

@thislooksfun thislooksfun self-assigned this Nov 24, 2020
@Schlabbi
Copy link
Author

This was tested on an iPhone 12 and an iPhone 8 Plus, both running iOS 14.2.1.

@thislooksfun
Copy link
Owner

Physical or simulator?

@Schlabbi
Copy link
Author

I only tested on physical devices.

@thislooksfun thislooksfun added critical Something is very wrong here bug Something isn't working labels Nov 24, 2020
@thislooksfun
Copy link
Owner

I was afraid of that. I'll look into this asap, thanks for letting me know!

@Schlabbi
Copy link
Author

Schlabbi commented Nov 24, 2020

I got a workaround.

When I change the @Published in the ViewModel Class to @State there won't be a high CPU usage.

As soon as it has the @Published property wrapper the updateUIViewController function will be called constantly.

Edit: When I use @State the variable won't be updated.

@thislooksfun
Copy link
Owner

I was able to reproduce the issue, and I have an idea what's causing it. If my hunch is correct, this is a bug in SwiftUI itself, though I haven't finished my analysis yet so it could be a bug in my code. In either case, the quick fix is to not store the search text in an observable object. Having an inline @State variable works as expected.

struct ContentView: View {
    
    @State var searchText = ""
    
    var body: some View {
        NavigationView {
            List {
                Text("foo")
            }.navigationBarSearch($searchText)
        }
    }
}

@thislooksfun
Copy link
Owner

@Schlabbi, this should be fixed in v1.2.3!

@JoeLago
Copy link
Contributor

JoeLago commented Nov 25, 2020

Was just trying to debug this behavior in my app. It seems the view keeps being rebuilt even when text isn't changing. You can set a breakpoint in the view and it will keep getting hit with no user interaction. This doesn't happen with a regular TextField eg.

    var body: some View {
        NavigationView {
            List {
                TextField("Input", text: $viewModel.searchText)
                Text("foo")
            }//.navigationBarSearch($viewModel.searchText)
        }
    }

@State won't work in my case since the UI needs to update when a published var changes.
The issue seems to persist in 1.2.3.

@thislooksfun
Copy link
Owner

@JoeLago are you sure this persists in 1.2.3? My testing showed that the cpu dropped to 0% at idle after the change.

@Schlabbi
Copy link
Author

You can reproduce it with the code that I shared originally, but it won't happen as soon as you open the screen.

If you initially open the screen the CPU usage will be at 0%.

However, this changes as soon as you type something into the search bar. Then the CPU usage will shoot up again and won't go down after a while.

@thislooksfun
Copy link
Owner

Strange, I saw the spike when typing when it always went back down to 0 after you stopped for me. I'll take another look.

@thislooksfun thislooksfun reopened this Nov 25, 2020
@Schlabbi
Copy link
Author

Looks like the CPU usage will only be high, if there is something typed in the searchbar. If you clear out the text it will be down to 0% again.

@thislooksfun
Copy link
Owner

@Schlabbi Sorry for the slow resolution, but I can now 100% confirm that this issue is resolved (both with text and without) in v1.2.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working confirmed critical Something is very wrong here
Projects
None yet
Development

No branches or pull requests

3 participants