|
Hi, I'm trying to show the current time with seconds and use .numericText transition for the text the problem is when second change it render all of the view and the numericText make CPU high how can I fix it? Thank you
|
|
|
|
|
|
@aviorrok claims:
I am not sure this is the case. @twoStraws has mentioned in several videos that SwiftUI is particularly clever at only changing the parts of the view that are directly affected by changing data. That is, SwiftUI won't redraw the entire screen on an iPad app, if the only thing that changed was the text in a text field. What makes you think that "it renders all of the view..." each time a second changes? Next, consider refactoring your code and moving the Text view to a separate SwiftUI file all together. Then add the component to your main view code. This way, you've moved the one-second update out of your larger parent view into an independent view all on its own. Profiling with InstrumentsFinally Xcode has a terrific set of tools called Instruments. Use this tool to allow Xcode to watch your code as it executes. You can measure how much memory is being used, how many cores. There's even an option to track SwiftUI itself and determine how many times the .body variable is called for each of your application's views. This may give you insight into what is getting redrawn when the second is updated in your text view. Search the google for "Profiling Xcode Instruments Donny Wals" for a nice article from March 2023. Keep CodingPlease return here and let us know how your solved your issue..... |
|
@Obelix Hi, The high CPU is due .animtion that trigger the .contentTransition(.numericText()) and make high CPU don't know why it use high CPU |
|
To update the clock seconds without rendering the entire view every second in SwiftUI, you can use a
By using a |
SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.