'SwiftUI Preview crash with await async iOS15 if returns a struct with UUID

I reduced to very simple code. If the struct has a Int is working, if there is an UUID the preview is crashing (it's working if I run on the simulator or real device)

Tested with iOS 15 Xcode 13.1 and beta 13.2

import SwiftUI
import Combine

struct MyStruct: Codable, Hashable, Identifiable {
    var id: UUID = UUID() //with Int is ok
    var str: String
}

struct ContentView2: View {
    
    @State private var myStruct: MyStruct = MyStruct(str: "struct1-init")
    
    var body: some View {
        VStack {
            Text(myStruct.str)
                .onAppear(perform: doSometingStruct)
        }
    }
    
    private func doSometingStruct() {
        Task {
            let get = await getAsyncStruct()
            myStruct = get
        }
    }
    
    private func getAsyncStruct() async -> MyStruct {
        let str = MyStruct(str: "struct1-done")
        return str
    }
}

struct ContentView2_Previews: PreviewProvider {
    static var previews: some View {
        ContentView2()
    }
}


Solution 1:[1]

It seems to be a bug of xcode to preview Views with async-code... Try to test with commented Task:

private func doSometingStruct() {
        //Task {
        //    let get = await getAsyncStruct()
        //    myStruct = get
        //}
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alex Kraev