Reputation: 32066
I just read on this answer(answer has since been removed) and I'm sure I've seen apps crash after hanging so what I read made sense.
If you block the main thread for too long the OS will kill your app.
However I wrote a few tests and found that none of them caused the app to crash after waiting approx 2-5 mins on each. Breakpoints confirmed that I'm running on the main thread.
Can someone confirm or disprove what I read please or have I just picked a lot of options which are non-blocking?
If I have picked non blocking options, can somebody explain why these are non-blocking?
while (true) { /*Nothing*/ }
while (true) { NSLog(@"nothing"); }
for(;;);
sleep(100000000);
while(true) { sleep(1); }
Upvotes: 2
Views: 1319
Reputation: 7344
The answer that you are referring is not correct, the crash is because he runs out of memory, not because he blocks the main thread. I believe that none of the examples that you've given block the thread. The way to really block the thread would be to use recursion, or call dispatch_sync from the main thread with main queue as argument. This would create the deadlock but I don't think your application will be terminated because of that (though I can be wrong).
So why are these non blocking? Depends how you define blocking. All of the examples that you provided except 4th only prevent your code from progressing to the next line, which is perfectly ok. The sleep(10000000)
is even weaker than this because it will properly return after given amount of seconds, so why would this cause any trouble?
Upvotes: 0
Reputation: 45598
iOS will only kill your app if it spends too long in some of the UIApplicationDelegate
methods like application:didFinishLaunchingWithOptions:
or applicationDidEnterBackground:
. You usually have 5 seconds to return, although this is not enforced for debug versions of your application.
Blocking the main thread outside of these methods will not cause your application to be terminated.
Upvotes: 2