Reading time: 1 min
This recipe shows how to troubleshoot common issues seen when trying to render SwiftUI previews in Xcode 12-15.
Occasionally, you'll encounter annoying issues while trying to preview your SwiftUI views in Xcode. This isn't a novel issue and has persisted in the past several versions of Xcode. At the time of writing this article, the current Xcode version is 15.1 and most of these issues are still present.
The preview issues usually come on one of these three forms:
- Cannot preview in this file, unexpected error occurred
- The compiler is unable to trype-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions.
- TimeoutError: Timed out waiting for a thunk to build after 30.0 seconds.
And here are a few things that usually work to troubleshoot the issues and make your previews work again:
- Build (
Cmd+B) the code before starting a preview. This usually works since, for whatever reason, building process that happens when you hit the preview button doesn't work the same way it does when started manually.
- Clear the build folder (
Shift+Cmd+K) and/or Derived Data folder. This one is a staple for a lot of Xcode issues and might work in this case as well.
- Disable automatic canvas refresh (
Editor -> Canvas -> uncheck Automatically Refresh Canvas). While this will make it so that changing the code won't automatically update the preview, it works like a charm most of the time, especially in Xcode 15+.
Lastly, especially in the case of the The compiler is unable... error, it might actually be helpful to break up the view into smaller views. This is also the only instance where the issue might be with your code instead of with the toolchain or the IDE.