Why main.sync crashes?

  1. Normally code gets executed on main thread, either synchronously or asynchronously.
  2. So if you write code main.sync
main.sync {
}

3. It will be like

main.async(sync) { //outer loop
    main.sync { //inner loop
    }
}

4. Because inner loop is sync, it waits outer loop to be done.

5. The outer loop can be done only after the inner loop is done.

6. Main queue is a serial queue.

7. So these two loops wait for each other to be done on serial queue. This is deadlock.

8. Crash occurs

10. So if you use main.sync on concurrent background queue, crash does not occur.

Reference

  1. https://rhino-developer.tistory.com/entry/ios-queuemainsync-쓸-때-나는-에러-알아보기-feat-deadlock
  2. https://ios-development.tistory.com/598

Changing custom alert -> main thread crash

[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {

completionHandler

The block to execute asynchronously with the results. This block may be executed on a background thread. The block has no return value and takes the following parameter:

I removed main async block when showing alert in custom alert file. And crashes accurred on the line showing alert in completion handler of get notification settings method.