SwiftUI previews stopped working everywhere after using EnvironmentObject

I added @EnvironmentObject to a property in a SwiftUI view as part of the official tutorial Adding User Interaction and added the .environmentObject modifier to the preview. However, I get the error:

Cannot preview in this file - MyApp.app may have crashed


This error now appears in every SwiftUI project, even ones without EnvironmentObject or a blank SwiftUI template, so I cannot use previews. I have tried rebooting several times, and made sure the command line tools are set to Xcode 11. How can I fix this? Thanks!

  • https://developer.apple.com/forums/thread/120541?answerId=681414022#681414022

Add a Comment

Replies

Did you do an option-clean-build Folder in XCode ?


Did you read the release notes of XCode 11.4:


SwiftUI Tutorials Known Issues

After following Section 7, Step 7 in the Building Lists and Navigation tutorial in Xcode, you might encounter an error in your projectʼs SceneDelegate.swift file. (51334559)

Workaround: Update line 14 of SceneDelegate.swift to use LandmarkList instead of LandmarkDetail:

window.rootViewController = UIHostingController(rootView: LandmarkList())


After following Section 5, Step 3 in the Handling User Input tutorial in Xcode, you might encounter an error in the preview provider. (51341785)

Workaround: Undo the change to the LandmarkDetail initializer, so that you only add the environmentObject(_:) modifier:


struct LandmarkDetail_Preview: PreviewProvider {     
     static var previews: some View {
          LandmarkDetail(landmark: landmarkData[0])
                .environmentObject(UserData())
} }


  • https://developer.apple.com/forums/thread/120541?answerId=681414022#681414022

Add a Comment

Thanks for the reply! I think that may help that specific app, but SwiftUI previews now crash in every SwiftUI project I have, including projects without EnivronmentObject. I reinstalled Xcode 11, but the problem persists.


The crash report contains this information:

Application Specific Information:
-[MTLSimBuffer initWithDevice:bufferRef:content:pointer:length:options:heap:deallocator:]:89: failed assertion `Invalid storageMode 1'

CoreSimulator 675 - Device: iPhone 8 (E3A5C531-2BD9-4EEE-BC8B-9AFAB54AECF8) - Runtime: iOS 13.0 (17A5534d) - DeviceType: iPhone 8

There seem to be a problem with simulator.


Could you either:

- select another device simulator

- remove and reinstall the erroneous one

The other preview devices I'm trying seem to have the same issue. I briefly had success running a preview on my physical iPad, but it stopped working. I'll try reinstalling the simulator.

Reinstalling the simulator didn't seem to work, and the crash logs still show that it's using a device I deleted. Are the SwiftUI previews different from the simulator?

Did you delete and reinstall ?


Could help

https://stackoverflow.com/questions/29076171/how-to-force-reinstall-ios-simulator

I deleted from the Devices and Simulators window, which modifies this directory.

~/Library/Developer/CoreSimulator/Devices

Then I reinstalled an iPhone X simulator, but the crash logs still state iPhone 8. I also previously previewed with an iPhone SE, which I didn't have installed as a simulator. It seems like SwiftUI previews are different from the simulator.

This output is produced by the error's diagnostics:

Landmarks.app crashed: Error Domain=render service Code=12 "Rendering service was interrupted" UserInfo={NSLocalizedDescription=Rendering service was interrupted}
I have this exact issue now!

Once I've added an @EnvironmentObject to my view, built the project, then removed it again, I cannot seem to get any previews working again.

Seems code-independent, as if a SwiftUI cache is still referring to the old Environment object.

@eos-pi: Have you found a solution in the meantime? I've also tried all the known XCode voodoo (didn't help) so it might only be solved by a clean install of MacOS? 😨
I had an similar issue to this one.

Turns out it was linked to me mapping a shortcut for Canvas > Automatically Refresh Canvas and hitting it pretty much every 10 seconds.
As soon as I remapped my shortcut to Canvas > Refresh Canvas instead the issue disappeared instantly.

Working on the tutorial on XCode 12.5.1 Getting error until the Step 7. This is how is solve the issue on LandmarkList

struct LandmarkList_Previews: PreviewProvider {
  static var modelData = ModelData()
  static var previews: some View {
    ForEach(["iPhone 12 Pro Max"], id: \.self) { deviceName in
      LandmarkList().environmentObject(modelData)
        .previewDevice(PreviewDevice(rawValue: deviceName))
        .previewDisplayName(deviceName)
    }
  }
}

Step 1: Delete everything from: ~/Library/Developer/Xcode/UserData/Previews/Simulator Devices/

Step 2: Run in the terminal: killall -9 com.apple.CoreSimulator.CoreSimulatorService

Kudos to the authors: https://stackoverflow.com/a/63384526/13407134

I'm seeing the same problem with Xcode 13.2.1, but the above steps isn't fixing it. Anyone seen a further workaround yet? I'm trying:

  1. Quit Xcode
  2. quit simulator
  3. delete files as above
  4. killall as above.

But I'm still getting preview not working.

  • Ah, figured it out. I have a build script that makes a dynamic file before sources are compiles with version & build info. The SwiftUI dependency analyzer gets confused and thinks this is too late and that I modified the source again and pauses updates due to source changes. Shot myself in the foot.

Add a Comment