SwiftUI Previews creating views out of thin air?

My iOS (iPhone/iPad/Mac Catalyst) app has a relatively complex authentication flow, primarily required due to it being an API client. Several of my views have .task modifiers that begin executing code when the views are created.

After moving to iOS 18 and Xcode 16, when running in the Simulator, I began noticing my workflow was breaking as views were being created out of sequence... but not by me 🤯

Here is an example stack when I place a breakpoint inside of this view's .task.

  • From my logging (and this stack) I can verify I am not creating this view.
  • This view has no preview associated with it, and there are no previews up the view hierarchy from it either.
  • This happens in the Simulator only

The only clue I have to go on is this line of text which is present at the top of what looks like ASM when I click on the (5) line in the stack above.

SwiftUI`(1) await resume partial function for dispatch thunk of static SwiftUI.PreviewModifier.makeSharedContext() async throws -> τ_0_0.Context:

If this (or any hierarchical view) used Previews, I'd remove them to see if it had an effect, but they don't! I'm at a loss on what to do here...

Answered by Slif in 811661022

This turned out to be an issue with state management on my part, unrelated to Swift / SwiftUI / Previews. Sorry for the trouble :(

I searched both my code any the only UI library I import N-u-k-eUI (sorry, Apple censors the work that sounds like "Nooke") for makeSharedContext and no findings :(

Since you are building and running in a simulator Previews isn't involved at all so that wouldn't be why this view is invoked. You say "created out of sequence". I'm not quite sure what sequence you mean here but moving forward with some assumptions I have some suggestions.

The task modifier schedules a task to run but there's no guarantee when it will or if another view currently on the screen will have its task run first. That might be what you mean by "out of sequence"?

What I would recommend is to put a breakpoint in the init of your views. If you are confused what part of the app is creating the views then the init would point to the culprit. The task modifier will be invoked long after a view is placed on screen so you won't see the culprit in the backtrace.

Since the call stack was collapsed in the screenshot, here's the expanded version. It looks like the SwiftUI Preview system up to something?

#0	0x00000001035e247c in closure #2 in AuthFirstCensusView.body.getter at /Users/vz/Documents/GitHub/reckoner-app/reckoner/Authentication/AuthFirstCensusView.swift:103
#1	0x00000001035e277c in partial apply for closure #2 in AuthFirstCensusView.body.getter ()
#2	0x00000001d1817138 in (1) await resume partial function for partial apply forwarder for closure #1 () async -> () in closure #1 (inout Swift.TaskGroup<()>) async -> () in closure #1 () async -> () in SwiftUI.AppDelegate.application(_: __C.UIApplication, handleEventsForBackgroundURLSession: Swift.String, completionHandler: () -> ()) -> () ()
#3	0x00000001d17b1e48 in (1) await resume partial function for dispatch thunk of static SwiftUI.PreviewModifier.makeSharedContext() async throws -> τ_0_0.Context ()
#4	0x00000001d19c10c0 in (1) await resume partial function for generic specialization <()> of reabstraction thunk helper <τ_0_0 where τ_0_0: Swift.Sendable> from @escaping @isolated(any) @callee_guaranteed @async () -> (@out τ_0_0) to @escaping @callee_guaranteed @async () -> (@out τ_0_0, @error @owned Swift.Error) ()
#5	0x00000001d17b1e48 in (1) await resume partial function for dispatch thunk of static SwiftUI.PreviewModifier.makeSharedContext() async throws -> τ_0_0.Context ()

Ah that helps! Thanks for the context. Let me make doubly sure I understand what you're doing. Are you clicking the run button to bring it up in the simulator? Or are you previewing in the Xcode canvas?

My developer buddy asked me to clarify that as well, literally 2m after you posted... is your name IZZY?? ;)

Indeed this only occurs in the Simulator (via the Run button as you mentioned), not inside of Xcode Previews. That's what's so confusing :)

Ok, this is definitely weird. I believe you. 😅 We'll need some deeper diagnostics here than you'd likely feel comfortable sharing in a public forum.

Could you please start a feedback at https://feedback.apple.com. Do a screen recording showing you starting the app and it stopping at the breakpoint with this backtrace. Capture the backtrace of all threads by typing bt all in the debugger and attach the output of that command to the feedback, along with the screen recording.

Then capture a sysdiagnose of your mac by running sudo sysdiagnose and attach that. Then also capture a sysdiagnose of the simulator you have booted (don't close it!) by running sudo simctl diagnose and attach that as well.

Post the feedback number here and we'll try to get to the bottom of this.

Accepted Answer

This turned out to be an issue with state management on my part, unrelated to Swift / SwiftUI / Previews. Sorry for the trouble :(

No trouble at all! Thanks for bringing this up and keeping us up to date. We're just glad you were able to sort things out.

SwiftUI Previews creating views out of thin air?
 
 
Q