I run the background job as follows:
UIApplication* application = [UIApplication sharedApplication]; _backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^ { [application endBackgroundTask:_backgroundTask]; _backgroundTask = UIBackgroundTaskInvalid; }];
The application is sent to the wallpaper , and everything is in order.
After a while, a certain condition is satisfied, and some object finishes executing this code:
[self performSelectorOnMainThread:@selector(doSomething) withObject:nil waitUntilDone:YES];
At this point, the application will work!
Note that the object runs in the main thread in this particular case.
I replaced the above code with the following:
dispatch_async(dispatch_get_main_queue(), ^{ [self doSomething]; });
And everything seems OK, the crash is gone.
What I can imagine is that waitUntilDone: YES may be the difference here, but that is just my gut feeling.
My question is:
Is it allowed to use performSelectorOnMainThread: withObject: waitUntilDone: YES when the application is running in the background?
If so, why did the application crash and why did dispatch_async solve the problem?
Thanks in advance!
source share