iPad Split View causing crash due to incorrect parent view controller in multitasking mode

I'm encountering a crash in my iPad application when using Split View or Slide Over multitasking mode. The crash is occurring with the error message:

Crashing on exception: child view controller:<UIPageViewController: 0x155f84200> should have parent view controller:<MyApp.HomeViewController: 0x29e040800> but actual parent is:<MyAoo.HomeViewController: 0x12d2c5e00>

The issue seems to be related to incorrect parent view controller assignments when retrieving the top view controller using the window. I've tried to address this by saving the persistent identifier in UserDefaults and comparing it when retrieving the key window, but the problem persists.

Here's a snippet of the code I'm using to retrieve the key window:

`static func getKeyWindow(for role: UISceneSession.Role = .windowApplication) -> UIWindow? {
    for scene in shared.connectedScenes {
        guard scene.session.role == role else { continue }

        if let savedIdentifier = UserDefaults.standard.string(forKey: "\(scene.session.persistentIdentifier)") {
            debugPrint("#### saved identifier is \(savedIdentifier)")
            if scene.session.persistentIdentifier == savedIdentifier {
                return (scene as? UIWindowScene)?.windows.first { $0.isKeyWindow }
            } else {
                debugPrint("#### didn't match identifier")
            }
        } else {
            return (scene as? UIWindowScene)?.windows.first { $0.isKeyWindow }
        }
    }

    return nil
}`

It seems like the issue arises when the app is opened in a split view, as the persistent identifier changes but the app fails to differentiate between the different instances. Consequently, it takes the wrong Split View window and returns the wrong HomeViewController instance, leading to the crash.

How can I properly handle the retrieval of the top view controller to avoid this crash in multitasking mode on iPad? Any insights or suggestions would be greatly appreciated.

Thanks in advance!