Swift gurus are these two equivalent?

Trying to get a ScrollView to jump back to the previous first item when new items are added and even in a test project this seems to be required before it works reliably!

This itself is in an .onChange modifier listening to the items if that helps with context

#swift #iOSdev

Follow

@jknlsn scrollTo isn't labeled as MainActor/Sendable, so I’d be wary of calling it from inside a Task.

It's likely your onChange is being fired before the view has had a chance to redraw, so there's nothing to scroll to yet.

@phill ah, thanks!

Any ideas on ways to prevent this until the view is redrawn? Or just .asyncAfter and hope for the best haha 🤔

@jknlsn @phill You can also mark the Task as running on the MainActor or use MainActor.run to manually dispatch to it.

@jknlsn @phill Just realized, when doing this from SwiftUI, your task will automatically inherit the MainActor context from the view. So it should be fine.

Sign in to participate in the conversation
The Not So Big Company

The home of The Not So Big Company on Mastodon.