0

I have a function that must be called from a non-main thread. In that thread, I am trying to determine status bar orientation like this:

   let currentInterfaceOrientation = UIApplication.shared.statusBarOrientation

But I obviously get a run time warning like this.

    UIApplication.statusBarOrientation() must be used from main thread only

My question is how to determine status bar orientation on secondary thread if you need it? Using DispatchQueue.main.sync is the right way but is risky for the reasons of potential deadlocks.

   var currentInterfaceOrientation = UIInterfaceOrientation.landscapeLeft

   DispatchQueue.main.sync {
                currentInterfaceOrientation = UIApplication.shared.statusBarOrientation
            }

What are the safe ways here using Swift?

Deepak Sharma
  • 5,577
  • 7
  • 55
  • 131

1 Answers1

1

I have a function that must be called from a non-main thread.

Add the orientation as an argument to this function.

messeb
  • 364
  • 2
  • 11
  • Problem is there is a chain of functions and this one sits at the top of the stack, all these functions are called on secondary thread. – Deepak Sharma Nov 11 '18 at 15:46
  • 1
    Could you give a small code example? I can't imagine that it is not possible to inject a dependency. To access `UIApplication.shared` directly is a little code smell. The easiest solution would be to create another global Object, where you set the orientation from `UIApplication.shared.statusBarOrientation` – messeb Nov 11 '18 at 15:56