11

I tried to search this out, but kinda stuck in this question. All guides about UI say, that all UI stuff should be on GCD main thread, but no one says about inner implementation of IBActions.

So, are IBActions fired on GCD main thread or not?

htzfun
  • 1,231
  • 10
  • 41
  • 2
    Yes, they are performed on main thread. – Rob Jan 14 '15 at 14:31
  • 1
    Yes, and you can test it by yourself using NSLog(@"is main thread? %d", [NSThread isMainThread]); You can also use the debugger and left view to know about what thread is been executed your code. – Ricardo Jan 14 '15 at 14:33
  • @Rob It's intuitive, but what about some proofs or documentation about this. – htzfun Jan 14 '15 at 14:34
  • 2
    @Ricardo Thanks, didn't think about such easy check=( Could you make this as answer, so that it would be useful for others? – htzfun Jan 14 '15 at 14:36
  • 1
    Done @htzfun, don't forget in these cases the debugger and left view is faster than using a log. – Ricardo Jan 14 '15 at 14:38
  • 3
    @htzfun, it is not intuitive, after a sec googling: _"As the name suggests, the main run loop executes on the app’s main thread. This behavior ensures that user-related events are processed serially in the order in which they were received."_ (source: https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html) – holex Jan 14 '15 at 15:09
  • Thanks, @holex. Well, sorry for my bad googling. Maybe, I didn't google the main run loop and app life cycle. – htzfun Jan 14 '15 at 20:44

2 Answers2

6

Yes, and you can test it by yourself using NSLog(@"is main thread? %d", [NSThread isMainThread]); You can also use the debugger and left view to know about what thread is been executed your code.

Popeye
  • 11,839
  • 9
  • 58
  • 91
Ricardo
  • 2,831
  • 4
  • 29
  • 42
  • 7
    Sure you can test it and it happens sometimes, but does the documentation guarantee that it will _always_ happen on the main queue? – Bryan Bryce Oct 02 '15 at 18:15
  • 1
    @BryanBryce, my answer responds to your comment. – ScottyBlades Jun 13 '18 at 04:13
  • The documentation doesn't guarantee anything about main-thread safety in the IBAction keyword. The only purpose of the IBAction which can be found in documentation is to connect to interface builder. Simply, the function whether IBAction or not will be called on thread which called the function. No guarantees... – Dominik Bucher May 19 '20 at 15:40
3

It is safe to assume that @IBActions are only called on the main thread by Apple's code under the hood. Apple's documentation:

Important: Use UIKit classes only from your app’s main thread or main dispatch queue, unless otherwise indicated. This restriction particularly applies to classes derived from UIResponder or that involve manipulating your app’s user interface in any way.

Note: it is entirely possible for your code to call an @IBAction's method on a background thread. That is why I make that distinction.

Note: If you are here because an error on an IBOutlet, Make your IBOutlets strong (not weak) to avoid accessing dereferenced IBOutlets in some cases.

ScottyBlades
  • 12,189
  • 5
  • 77
  • 85
  • It's definitely not safe to assume that IBAction marked func is called on the main thread under the hood. The keywords only purpose is to make the function connectable to interface builder. Otherwise the function will be called from thread which called the function before. Therefore if Apple goes crazy and the place from there the @IBAction was called is not on main thread, you are not on main thread :) – Dominik Bucher May 19 '20 at 15:42
  • 1
    Hi @Dominik Bucher , can you please provide an example that you are aware of where IBAction marked functions are called from a background thread under the hood or any reasoning why Apple would "go crazy" and call an IBAction under the hood on a background thread against their own "Important" marked guidelines? – ScottyBlades May 20 '20 at 03:58