6

I receive this error whenever I try to preview a freshly created swiftui view.

messageRepliedWithError("Connecting to launched interactive agent 9297", Optional(Error Domain=com.apple.dt.ultraviolet.service Code=17 "connectToPreviewHost: Failed to connect to 9297: Error Domain=com.apple.dt.ProcessAttachUtilities Code=3 "Target process 9297 exited prematurely, likely crashed" UserInfo={NSLocalizedDescription=Target process 9297 exited prematurely, likely crashed}" UserInfo={NSLocalizedDescription=connectToPreviewHost: Failed to connect to 9297: Error Domain=com.apple.dt.ProcessAttachUtilities Code=3 "Target process 9297 exited prematurely, likely crashed" UserInfo={NSLocalizedDescription=Target process 9297 exited prematurely, likely crashed}}))

enter image description here

I have no idea how to fix this so I can see previews again. I am on the latest Catalina 10.15.1

Rob
  • 757
  • 1
  • 10
  • 26
  • You should show the code that that is causing the crash rather than just showing the crash log. – Andrew Oct 31 '19 at 15:18
  • It’s fresh SwiftUI from the File -> New. Untouched swiftui view structure – Rob Oct 31 '19 at 15:21
  • Is this in a brand new project? Or have you already been working on this project? – Andrew Oct 31 '19 at 15:37
  • I’ve been working on this project. It is a mixed UIKit and SwiftUI project. I swear the preview used to work previously too, but as lately I was working with UIKit, I can’t say when this started. A brand new project, the preview works as normal. – Rob Oct 31 '19 at 15:39

4 Answers4

14

I found the Preview Canvas to be extremely buggy as well. Or maybe swiftUI code has a lot of undesired side effects.

In many occasions I found that Clean Build Folder, then kill Xcode, start Xcode, do a full Build may (sometimes) fix the problems. Until I make a tiny change somewhere... Then the whole circus starst again.

In practice this is very time consuming and basically unworkable. I found it more efficient to not use the preview canvas at all and just run the code. It is quicker, more reliable and in the end saves lots of time and frustration.

At the same time I think it is a bit of a shame on Apple. Most frustrating of all (for me at least) is that documentation is virtually non existing. It is all trial and error. Mostly error.

In short: my experience is that it is better not to use swiftUI Previews for anything beyond the very basic (yet).

Johannes
  • 307
  • 2
  • 11
  • Having exact same problem, just using two Text Elements inside one VStack, deep cleaning and reloading Xcode worked. Also a couple of times killing the canvas and just using the editor and then opening the canvas again worked, but was just a few times (2 of 10 tries during the whole testing session), rest got the same error. – Wilson Muñoz Dec 04 '19 at 16:57
6

I had the same problem with mine, I went through a couple of steps, so I will mention everything I did hopefully it helps someone.

But it's worth mentioning that I think the error was caused by Xcode trying to connect to the preview which failed all the time, because I was using an iPhone 8 preview.

Also I was able to use iPhone 8 preview after a while again. This looks like a buggy Xcode.

Steps. 1. Clean build folder and your derived data. 2. Close XCode Completely. **Key Step. Open Xcode again and change your preview to iPhone XS or iPhone 11. See the image below.

enter image description here

GyroCocoa
  • 1,542
  • 16
  • 19
  • Not sure if I'm just imagining it, but I've had far fewer SwiftUI preview errors once I set my target to iPhone XS. Also, `Command+Option+P` is your friend. – bcause Dec 08 '19 at 22:21
  • Xcode 11.1 was a bit buggy. but I also stopped getting those weird glitches recently. – GyroCocoa Dec 11 '19 at 12:26
1

I had same issue. It was very confusing. I am using FirebaseFirestore pod in my podfile and when I remove FirebaseApp.configure() from AppDelegate didFinishLaunchingWithOptions function preview works.

I have done this 10 times just to make sure that this is an issue, and it was issue for me. So probably any code from Firbase added in that function was problem for me.

So, by checking logs I came across this error:

Crashed Thread: 3 Dispatch queue: com.google.GDTCORFlatFileStorage

Also googling I came across those bugs:

https://github.com/firebase/firebase-ios-sdk/issues/5707

https://github.com/firebase/firebase-ios-sdk/issues/5708

Kenan Begić
  • 1,228
  • 11
  • 21
0

Turning off the thread sanitizer worked for me (I was just playing around with settings against another project that worked), so there's one more thing to try...

lzl
  • 469
  • 4
  • 10
  • And it crashed again, this time I looked at the crash log and it repeatedly crashes at piece of code that did an assertFailure when a downloaded image was nil. Except I wasn't downloading any images I was simply running a system generated hello world swiftui preview. Confused... but I changed the assertion to debugPrint and the preview runs again..... so tip num2.... check the crash log.... – lzl May 28 '20 at 19:08