UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

NavigationLink slow response on second tap

Forums > SwiftUI

Hi, struggling through writing my first SwiftUI app (previously written a couple of UIKit apps about 7 years ago) and have run into an issue that I can't seem to solve

After initial lauch of the app if you tap on one of the List items it navigates to the detail view immediately, shows the loading page and then the completed view. After then navigating back to the List view the second selection of an item pauses on the List view before showing the loading page then the detail screen.

I'm loading data via onAppear() on the detail screen, if I remove the data call the navigation works without pauses. It's almost like I want a hasAppeared() method to call the data!

I'm already using onReceive() with a NotificationCenter willEnterForegroundNotification for reloading data when app comes to the foreground, can I get a notification when my view has actually appeared from the NavigationLink as a way around this pause, or is there away to get rid of the pause in onAppear() ?

3      

Is this on simulator? If so have you tried it a real device. Sometime the simulator is not the quickest

3      

Yes, same on device,someone elsewhere suggests my onAppear maybe blocking the main thread, though why it works differently between the first request and second request confuses me

3      

Why do you not try .onDismiss on the detail view and set the data back to what ever the app is loaded. I do not know if that will work!

3      

Thanks, it's an @EnvironmentObject so not sure whether you can set the data back, and it doesn't sound the very SwiftUI thing to do, investigating urlsession.shared.datataskpublisher rather than the urlsession.shared.datatask that I'm using, as I feel this is whats blocking the UI

3      

Turns out....

after re writing my URLSession stack to use dataTaskPublisher the issue still persisted, so after a lot of digging with Instruments it was down to creating 7 * 168 Calendar instances on every view, they're quite expensive! Using a static DateFormatter has solved my issue. Still confused as to why the first run didn't display this issue, but at least my GUI now behaves as it should.

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.