Swift OSX- View Loading Late (viewDidLoad) (applicationDidFinishLaunching)

69 Views Asked by At

Thanks for the help!

Whenever I enter any code after viewDidLoad or applicationDidFinishLaunching, my view is delayed until after that code has finished. Also tried viewDidAppear.

*Not Using SwiftUI *Running Big Sur Xcode 13.2.1

Let's say I've entered

override func viewDidLoad() {
    super.viewDidLoad()
    
sleep(5) }

The view will load after the sleep... I know sleep isn't a great example, but I've tried it with many functions.

Also if I run any code in AppDelegate - in applicationDidFinishLaunching Same Result.

Any Tips/ Help/ Updates are greatly appreciated!

1

There are 1 best solutions below

2
Ivan Vorobyev On

You can use DispatchQueue to resolve that issue.

Run your code in another thread using your own queue or predetermined:

override func viewDidLoad() {
    super.viewDidLoad()
    
    DispatchQueue.global(qos: .utility).async {
        //your code here
        sleep(5)
    }
}