Rakesh
Rakesh

Reputation: 1

Getting a runtime error in Swift 4.2 for an existing Objective-C code

This is the error:

-[UIApplication statusBarOrientation] must be used from a main thread only

statusBarOrientation is used in two places in my code.

  1. [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(positionHUD:) 
                          name:UIApplicationDidChangeStatusBarOrientationNotification
                          object:nil];
    

This one is in Objective-C .m file ^^

  1. UIInterfaceOrientation orientation = UIApplication.sharedApplication.statusBarOrientation;
    

Please help with any clue or work around for this. Thanks!

Upvotes: 0

Views: 76

Answers (1)

gog
gog

Reputation: 1367

It is unlikely that the problem is coming from (1); the error is likely coming from you reading the statusBarOrientation member from a thread other than the main one.

This is how you can read the statusBarOrientation from the main thread:

dispatch_async(dispatch_get_main_queue(), ^(void){
    // Now, we are in the context of the main (GUI) thread. You can perform any GUI updates here.
    self.frame = [[[UIApplication sharedApplication] delegate] window].bounds;
    UIInterfaceOrientation orientation = UIApplication.sharedApplication.statusBarOrientation;
    // Put any other code that makes use of the "orientation" variable inside here

});

From here on, it all depends on what you want to do with the orientation variable.

Upvotes: 1

Related Questions