SwiftUI macOS DocumentGroup always crash when launching


stack info:
Code Block objc
Thread 1 Queue : com.apple.main-thread (serial)
#0 0x00007fff2cca2d75 in swift_isUniquelyReferenced_nonNull_native ()
#1 0x00007fff4263ec1c in specialized Array._makeUniqueAndReserveCapacityIfNotUnique() ()
#2 0x00007fff42abe6ad in PlatformDocumentController.createDocumentClassIfNeeded(_:) ()
#3 0x00007fff42abdf97 in PlatformDocumentController.init(formulas:) ()
#4 0x00007fff4290d0ce in AppDelegate.applicationWillFinishLaunching(_:) ()
#5 0x00007fff4290d880 in @objc AppDelegate.applicationWillFinishLaunching(_:) ()
#6 0x00007fff20638fec in __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ ()
#7 0x00007fff206d489b in ___CFXRegistrationPost_block_invoke ()
#8 0x00007fff206d480f in _CFXRegistrationPost ()
#9 0x00007fff20609bde in _CFXNotificationPost ()
#10 0x00007fff21378abe in -[NSNotificationCenter postNotificationName:object:userInfo:] ()
#11 0x00007fff22e52931 in -[NSApplication finishLaunching] ()
#12 0x00007fff22e5253a in -[NSApplication run] ()
#13 0x00007fff22e2696f in NSApplicationMain ()
#14 0x00007fff4261a9f4 in specialized runApp(_:) ()
#15 0x00007fff42eb1792 in runApp<A>(_:) ()
#16 0x00007fff42a3009d in static App.main() ()

crash info:
Code Block objc
SwiftUI`merged generic specialization <SwiftUI.HostPreferencesCombiner.Child> of Swift.Array._makeUniqueAndReserveCapacityIfNotUnique() -> ():
  0x7fff4263ec00 <+0>: pushq %rbp
  0x7fff4263ec01 <+1>: movq  %rsp, %rbp
  0x7fff4263ec04 <+4>: pushq %r15
  0x7fff4263ec06 <+6>: pushq %r14
  0x7fff4263ec08 <+8>: pushq %rbx
  0x7fff4263ec09 <+9>: pushq %rax
  0x7fff4263ec0a <+10>: movq  %rdx, %r14
  0x7fff4263ec0d <+13>: movq  %rsi, %r15
  0x7fff4263ec10 <+16>: movq  %rdi, %rbx
  0x7fff4263ec13 <+19>: movq  (%r13), %rdi
  0x7fff4263ec17 <+23>: callq 0x7fff430225a8      ; symbol stub for: swift_isUniquelyReferenced_nonNull_native
-> 0x7fff4263ec1c <+28>: testb %al, %al
  0x7fff4263ec1e <+30>: je   0x7fff4263ec2b      ; <+43>
  0x7fff4263ec20 <+32>: addq  $0x8, %rsp
  0x7fff4263ec24 <+36>: popq  %rbx
  0x7fff4263ec25 <+37>: popq  %r14
  0x7fff4263ec27 <+39>: popq  %r15
  0x7fff4263ec29 <+41>: popq  %rbp
  0x7fff4263ec2a <+42>: retq  
  0x7fff4263ec2b <+43>: movq  (%r13), %rax
  0x7fff4263ec2f <+47>: movq  0x10(%rax), %rsi
  0x7fff4263ec33 <+51>: incq  %rsi
  0x7fff4263ec36 <+54>: pushq $0x1
  0x7fff4263ec38 <+56>: popq  %rdx
  0x7fff4263ec39 <+57>: xorl  %edi, %edi
  0x7fff4263ec3b <+59>: movq  %rbx, %rcx
  0x7fff4263ec3e <+62>: movq  %r15, %r8
  0x7fff4263ec41 <+65>: callq *%r14
  0x7fff4263ec44 <+68>: jmp  0x7fff4263ec20      ; <+32>
  0x7fff4263ec46 <+70>: nopw  %cs:(%rax,%rax)


swiftui code

Code Block swift
DocumentGroup(newDocument: {
      return MyFileDocument(self.home.create())
    }()) { file in
      if let m = home.open(file) {
        MainView(vModel: m ).background(
          HostingWindowFinder { (window) in
            guard let window = window else {
              return
            }
            window.delegate = home
            print(window)
          }
        )
      } else {
        Text("ERROR")
      }
    }.commands {
      SaveCommands()
      SidebarCommands()
    }



i found the reason:
ForEach is asynchronous!
Code Block swift
Button("Open...") {
        NSDocumentController.shared.openDocument(nil)
      }.keyboardShortcut("O", modifiers: .command)
       
      MenuButton("Open Recent") {
        let recents = NSDocumentController.shared.recentDocumentURLs
        if recents.count > 0 {
          ForEach(recents, id: \.self) { url in
            Button(url.lastPathComponent) {
              home.open(url: url, force: true)
            }
          }
        }
        Divider()
        Button("Clear Menu") {
          NSDocumentController.shared.clearRecentDocuments(nil)
        }
      }
       
      Button("Open Quickly...") {
      }.keyboardShortcut("O", modifiers: [.command,.shift])


SwiftUI macOS DocumentGroup always crash when launching
 
 
Q