11

Seems like previews stopped working on Xcode 12! Trying to preview the SwiftUI file template and getting the error below. Any ideas how to fix this? Tried cleaning the build folder, deleting derived data and restarting Xcode to no avail!

On Catalina 10.15.6.

RemoteHumanReadableError: Failed to update preview.

The preview process appears to have crashed.

Error encountered when sending 'prepare' message to agent.

==================================

|  RemoteHumanReadableError: The operation couldn’t be completed. (BSServiceConnectionErrorDomain error 3.)
|  
|  BSServiceConnectionErrorDomain (3):
|  ==BSErrorCodeDescription: OperationFailed

The code I am trying to preview (from SwiftUI new file template):

import SwiftUI

struct SwiftUIView: View {
    var body: some View {
        Text("Hello, World!")
    }
}

struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView()
    }
}
Zorayr
  • 23,770
  • 8
  • 136
  • 129
  • 1
    Seems like an Xcode error, cleaning derived data might help or just restarting Xcode – Itay Brenner Sep 24 '20 at 02:17
  • Deleted all that I could, derived data, build folder.& restarted Xcode several times, nope, still not working! – Zorayr Sep 24 '20 at 02:27
  • Could it be that you enabled Preview on Device? I know it isn’t helpful but that Xcode error may happen due to lots of reasons – Itay Brenner Sep 24 '20 at 02:56
  • I toggled preview on device on/off, but still can't get it to work. Actually, seems like this is a common issue with Xcode 12, lots of people reporting preview issues but no solution in the horizon! – Zorayr Sep 24 '20 at 03:06
  • 1
    Did you try to erase Xcode Application state beside the derived data? The Xcode Application State is saved on path `~/Library/Saved\ Application\ State/com.apple.dt.Xcode.savedState` – kerim.ba Sep 24 '20 at 13:05
  • @kerim.ba saved application state is deleted when Xcode and Simulator exit, so those folders are fresh at every launch. At least with Xcode 13 they are. – Jeff Jan 06 '22 at 20:56

6 Answers6

6

If your SwiftUIView use ObservableObject as environmentObject, try this:

struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView().environmentObject(YourObservableObjectClass())
    }
}
Elletlar
  • 3,136
  • 7
  • 32
  • 38
synix
  • 69
  • 3
3

Apparently it was a bug with Firestore, which is most likely the cause: XCode 12 Preview Crashes when adding Firebase SDK Swift UI 2.0. The best solution at this point is to call, pod update which should solve the issue. I verified on Xcode 12, iOS 13+.

Stanford
  • 333
  • 2
  • 10
1

if you use SwiftUI (ObservableObject, @EnvironmentObject)

add to previews

.environmentObject(ModelData())

--

struct LandmarkDetail_Previews: PreviewProvider {
        static var previews: some View {
    
            LandmarkDetail(landmark: ModelData().landmarks[1])
                .environmentObject(ModelData())
        }
    }

my ModelData

final class ModelData:ObservableObject
{
   @Published var landmarks: [Landmark] = load("landmarkData.json")
}
Erhan Demirci
  • 4,173
  • 4
  • 36
  • 44
0

SwiftUI preview error

I managed to solve this problem. It is enough to comment everything in the didFinishLaunchingWithOptions method. Then when you preview SwiftUI it will work. Solution

TylerH
  • 20,799
  • 66
  • 75
  • 101
Sosin Vitalii
  • 339
  • 3
  • 4
0

Error: Cannot preview in this file

The comment by @kerim.ba worked for me:

Did you try to erase Xcode Application state beside the derived data? The Xcode Application State is saved on path ~/Library/Saved\ Application\ State/com.apple.dt.Xcode.savedState

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
efr
  • 1
-3

I had the same issue when I updated Firebase Analytics (to v6.33.0). Commenting out the following line fixed it.

FirebaseApp.configure()
Al Priest
  • 183
  • 3
  • 7
  • No. It's 100% reproducible with that call to Firebase included. Copy your code above into a brand-new project, does that work? – Al Priest Sep 27 '20 at 08:41
  • Didn't work for me, it's super strange. Now getting error, `MessageSendingError: Connection interrupted: send previewInstances message to agent`. – Zorayr Oct 02 '20 at 22:21
  • As mentioned by Stanford above, this is no longer an issue in Firebase v6.34.0. – Al Priest Oct 10 '20 at 14:20