0

Im having an issue using live previews on xcode. The project builds fine and works on a simulator. I have all my environmentobjects connected so I am a bit confused. And yes I also have the EO on the app file itself.

"Cannot preview in this file - Message send failure for send display message to agent"

I am using AlertSettingsView in every view located in TabBarView - HomeView, SearchView, ExploreView, and OrdersView. When using the AlertSettings preview provider everything works just fine. But when I try to preview the entire app it no longer works. If I remove the highlighted lines below the code will preview just fine.

struct AlertSettingsView: View {
    @EnvironmentObject var avm: AlertsViewModel
    private let width: CGFloat = UIScreen.main.bounds.width
    private let height: CGFloat = UIScreen.main.bounds.height
    var body: some View {
        ZStack {
            Color.theme.orange.ignoresSafeArea()
            VStack {
                ForEach(0..<8) { text in
                    Text("lakjsdfiojas;dfkljadskl;jfj")
                }
            }
        }
        .frame(width: width, height: height/2)
        .animation(.spring())
        .cornerRadius(15)
        .offset(y: avm.settingsViewOffset) <-- This line breaks the preview
    }
}
class AlertsViewModel: ObservableObject {
    @Published var showAlertsView: Bool = true
    @Published var showSettingsView: Bool = false
    @Published var settingsViewOffset: CGFloat = UIScreen.main.bounds.height/2
}
struct TabBarView: View {
    @EnvironmentObject var tvm: TradeViewModel
    @State private var selectedTab: Int = 0
    var body: some View {
        ZStack {
            Color.theme.background.ignoresSafeArea()
            VStack(spacing: -4) {
                ZStack {
                    switch selectedTab {
                    case 0:
                        HomeView()
                    case 1:
                        SearchView()
                    case 2:
                        ExploreView()
                    case 3:
                        OrdersView()
                    default:
                        HomeView()
                    }
                }
                Spacer()
                TabBarButtons(selectedTab: $selectedTab)
            }
            .ignoresSafeArea(.keyboard, edges: .bottom)
            
            HomeMenuView()
        }
        .fullScreenCover(isPresented: $tvm.showTradeView) {
            TradeView()
        }
    }
}

struct TabBarView_Previews: PreviewProvider {
    @State static var avm = AlertsViewModel()
    @State static var hvm = HomeViewModel()
    @State static var ipvm = InvestorProfileViewModel()
    @State static var apvm = ArtistProfileViewModel()
    @State static var tvm = TradeViewModel()
    static var previews: some View {
        TabBarView()
            .environmentObject(avm)
            .environmentObject(hvm)
            .environmentObject(ipvm)
            .environmentObject(apvm)
            .environmentObject(tvm)
            .preferredColorScheme(.dark)
    }
}
Trevor
  • 580
  • 5
  • 16
  • This code is impossible to debug since it relies on types that you haven't included in your code here. – jnpdx Nov 19 '21 at 21:53
  • Well since its a preview provider issue this is technically the only necessary code? Its the only preview im having an issue with. – Trevor Nov 20 '21 at 00:20
  • You haven’t included any of the view models besides the alerts one. You should include a [mre] where others can experience the issue if you want a better shot at a good answer. – jnpdx Nov 20 '21 at 00:22
  • There is a period at the end of the line, likely a typo and may be the issue – lorem ipsum Nov 20 '21 at 06:45

1 Answers1

0

Have you tried turning off the "Automatically Refresh Canvas" option under Xcode >> Editor >> Canvas >> "Automatically Refresh Canvas"?

--Update on 12th Jan--

After receiving an email from Apple that they think that it's a bug, and they will need more time/and another ticket to find workaround, I have found a reproducible temporary workaround from my end. While it may not work for everyone, I believe it could help those using M1 Macbook Pro, various cocoapods and excluding Arm64 architecture as it involves uncheck/checking "Open with Rosetta".

Please see my other answer in another almost identical SO question -
Cannot preview in this file -- Message send failure

Michael
  • 370
  • 3
  • 11