SwiftUI: NSInternalInconsistencyException _UIDatePickerContainerViewController

We had a crash in our app which got reported on sentry. This crash happened in the latest iOS 16.5.1.

We've a view developed with SwiftUI which is loaded using UIHostingViewController inside an app developed with UIKit.

Can someone guide me here what could be the reason for this as this is not reproducible?

This UITargetedPreview initializer requires that the view is in a window, but it is not. Either fix that, or use the other initializer that takes a target. (view: <UIDatePickerIOSCompactView: 0x105033150> => <UIDatePicker: 0x1050103e0> => <TtGC7SwiftUI16PlatformViewHostGVS_P10$1b3477ba832PlatformViewRepresentableAdaptorVS_P10$1b34536b028UIKitDatePickerRepresentable: 0x105011390>)

OS Version: iOS 16.5.1 (20F75) Report Version: 104

Exception Type: EXC_CRASH (SIGABRT) Crashed Thread: 0

Application Specific Information:
This UITargetedPreview initializer requires that the view is in a window, but it is not. Either fix that, or use the other initializer that takes a target. (view: <_UIDatePickerIOSCompactView: 0x105033150> => <UIDatePicker: 0x1050103e0> => <_TtGC7SwiftUI16PlatformViewHostGVS_P10$1b3477ba832PlatformViewRepresentableAdaptorVS_P10$1b34536b028UIKitDatePickerRepresentable__: 0x105011390>)

Thread 0 Crashed:
0   CoreFoundation                  0x3354f7cb4         0x3354ee000 + 164
1   libobjc.A.dylib                 0x3276203cc         0x327608000 + 56
2   Foundation                      0x329f43568         0x329a62000 + 184
3   UIKitCore                       0x339e48ad0         0x3395cc000 + 184
4   UIKitCore                       0x33a5c01ec         0x3395cc000 + 556
5   UIKitCore                       0x33a5bf790         0x3395cc000 + 196
6   UIKitCore                       0x33a5becc0         0x3395cc000 + 64
7   UIKitCore                       0x33a5bf514         0x3395cc000 + 36
8   UIKitCore                       0x339844a04         0x3395cc000 + 1120
9   UIKitCore                       0x339719eb8         0x3395cc000 + 40
10  UIKitCore                       0x339d310f8         0x3395cc000 + 316
11  UIKitCore                       0x33987e49c         0x3395cc000 + 112
12  UIKitCore                       0x33976c434         0x3395cc000 + 68
13  UIKitCore                       0x33976c368         0x3395cc000 + 164
14  UIKitCore                       0x33976c27c         0x3395cc000 + 492
15  UIKitCore                       0x33960abe4         0x3395cc000 + 104
16  UIKitCore                       0x339ac9c14         0x3395cc000 + 68
17  UIKitCore                       0x339c1a4ac         0x3395cc000 + 80
18  UIKitCore                       0x33a27ec88         0x3395cc000 + 140
19  UIKitCore                       0x33a27e1e4         0x3395cc000 + 88
20  CoreFoundation                  0x3355c1124         0x3354ee000 + 24
21  CoreFoundation                  0x3355cd7b0         0x3354ee000 + 172
22  CoreFoundation                  0x3355525e4         0x3354ee000 + 240
23  CoreFoundation                  0x3355680d0         0x3354ee000 + 824
24  CoreFoundation                  0x33556d3e8         0x3354ee000 + 608
25  GraphicsServices                0x3abf25358         0x3abf24000 + 160
26  UIKitCore                       0x3399696e4         0x3395cc000 + 884
27  UIKitCore                       0x339969348         0x3395cc000 + 336
28  ABC                 0x203081088         0x202edc000 + 1724552
29  <unknown>                       0x1cda1adec         0x0 + 7744892396


Below is the SwiftUI code for DatePicker

struct CustomDatePickerView: View {
    @SwiftUI.Binding var diaryEntryDate: Date
    @ObservedObject var viewModel: DiaryEntryViewModel

    var body: some View {
        HStack(spacing: 2) {
            DatePicker("",
                       selection: $diaryEntryDate,
                       in: ...Date(),
                       displayedComponents: .date)
            .id(diaryEntryDate)
            .environment(\.locale, Locale(identifier: "us"))
            .clipped()
            .labelsHidden()
            .foregroundColor(Color(.primaryDarkColor ?? .blue))
            .colorInvert()
            .colorMultiply(Color(UIColor.primaryDarkColor ?? .black))
            Image("arrowDropDown")
                .frame(width: 10, height: 5)
                .onChange(of: diaryEntryDate) { newValue in
                    viewModel.calendarDate = diaryEntryDate
                }
        }
        .frame(maxWidth: .infinity, maxHeight: 52.0)
        .accentColor(Color.black)
        .background(Color(.primaryLightColor ?? .blue))
    }
}
SwiftUI: NSInternalInconsistencyException _UIDatePickerContainerViewController
 
 
Q