Skip to content

Xcode Preview – BSServiceConnectionErrorDomain error / crash

TL;DR Solution

Add .environmentObject(AppData()) modifier to the preview struct.

Preview Crash

Recently, I ran into an issue where I couldn’t preview my changes after introducing non-static content. The error in question is Cannot preview in this file -- The operation couldn't be completed. (BSServiceConnectionErrorDomain error 3.)

Naturally the next step is running the diagnostics which was a bit hairy because the Reveal in Finder didn’t actually open the correct directory.

After a little digging the I was able to find the previews-diagnostics-... directory and thus the actual crash log. The crash log was a bit more straightforward and reveal the issue just not where I needed to add the .environmentObject(AppData()) modifier. The app already had the .environmentObject(AppData()) added to the top view ContentView but not the Preview

Solution

Turns out all that was missing was adding .environmentObject(AppData()) modifier to my preview. Note, this ends up being added to any view that uses the view in question.

Leave a Reply

Your email address will not be published. Required fields are marked *