NSInternalInconsistencyException related to NSAppearance while modifying a navigation bar appearance

I have a problem. Can you tell me how to fix it. Thank Please take a look into the crash and Please help if you can find the reason behind it.

Fatal Exception: NSInternalInconsistencyException and crashed: com.twitter.crashlytics.ios.exception

Fatal Exception: NSInternalInconsistencyException
0  CoreFoundation                 0x184faf164 __exceptionPreprocess
1  libobjc.A.dylib                0x1841f8528 objc_exception_throw
2  CoreFoundation                 0x184faf038 +[NSException raise:format:]
3  Foundation                     0x1859497f4 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:]
4  UIKit                          0x18ee6c3b8 +[NSObject(UIAppearanceAdditions) _installAppearanceSwizzlesForSetter:]
5  UIKit                          0x18ee6ed0c -[_UIAppearance _beginListeningForAppearanceEventsForSetter:]
6  UIKit                          0x18ee6efb4 -[_UIAppearance _handleSetterInvocation:]
7  CoreFoundation                 0x184fb481c ___forwarding___
8  CoreFoundation                 0x184e99ccc _CF_forwarding_prep_0
9  MasterProjectAPI               0x102ac0cd4 PivotshareHelper.setUpDesign() -> () (PivotshareHelper.swift:304)
10 MasterProjectAPI               0x102acace8 specialized PivotshareHelper.(getChannel() -> ()).(closure #1) (PivotshareHelper.swift:14)
11 MasterProjectAPI               0x102ae6764 partial apply for PivotshareHelper.(getAuthors() -> ()).(closure #1)
12 MasterProjectAPI               0x102b62b70 Data?URLResponse?Error? (VidappHelper.swift)
13 CFNetwork                      0x185539d68 __75-[__NSURLSessionLocal taskForClass:request:uploadFile:bodyData:completion:]_block_invoke
14 CFNetwork                      0x1855526bc __49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke
15 Foundation                     0x185985ba0 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__
16 Foundation                     0x1858c5894 -[NSBlockOperation main]
17 Foundation                     0x1858b54c4 -[__NSOperationInternal _start:]
18 libdispatch.dylib              0x18492ea14 _dispatch_client_callout
19 libdispatch.dylib              0x184936200 _dispatch_block_invoke_direct$VARIANT$mp
20 libdispatch.dylib              0x18492ea14 _dispatch_client_callout
21 libdispatch.dylib              0x184936200 _dispatch_block_invoke_direct$VARIANT$mp
22 libdispatch.dylib              0x1849360ac dispatch_block_perform$VARIANT$mp
23 Foundation                     0x185987878 __NSOQSchedule_f
24 libdispatch.dylib              0x18492ea14 _dispatch_client_callout
25 libdispatch.dylib              0x184936f08 _dispatch_continuation_pop$VARIANT$mp
26 libdispatch.dylib              0x18493580c _dispatch_async_redirect_invoke$VARIANT$mp
27 libdispatch.dylib              0x18493bcf4 _dispatch_root_queue_drain
28 libdispatch.dylib              0x18493ba38 _dispatch_worker_thread3
29 libsystem_pthread.dylib        0x184bd706c _pthread_wqthread
30 libsystem_pthread.dylib        0x184bd6b6c start_wqthread`


crashed: com.twitter.crashlytics.ios.exception
0  MasterProjectAPI               0x102ce762c CLSProcessRecordAllThreads (CLSProcess.c:376)
1  MasterProjectAPI               0x102ce762c CLSProcessRecordAllThreads (CLSProcess.c:376)
2  MasterProjectAPI               0x102ce7aa0 CLSProcessRecordAllThreads (CLSProcess.c:407)
3  MasterProjectAPI               0x102cd83b4 CLSHandler (CLSHandler.m:26)
4  MasterProjectAPI               0x102ce5c58 __CLSExceptionRecord_block_invoke (CLSException.mm:199)
5  libdispatch.dylib              0x18492ea14 _dispatch_client_callout + 16
6  libdispatch.dylib              0x184937618 _dispatch_queue_barrier_sync_invoke_and_complete + 56
7  MasterProjectAPI               0x102ce56f4 CLSExceptionRecord (CLSException.mm:206)
8  MasterProjectAPI               0x102ce5524 CLSExceptionRecordNSException (CLSException.mm:102)
9  MasterProjectAPI               0x102ce5134 CLSTerminateHandler() (CLSException.mm:259)
10 libc++abi.dylib                0x1841e854c std::__terminate(void (*)()) + 16
11 libc++abi.dylib                0x1841e85b8 std::terminate() + 60
12 libobjc.A.dylib                0x1841f876c _destroyAltHandlerList + 10
13 libdispatch.dylib              0x18492ea28 _dispatch_client_callout + 36
14 libdispatch.dylib              0x184936200 _dispatch_block_invoke_direct$VARIANT$mp + 288
15 libdispatch.dylib              0x18492ea14 _dispatch_client_callout + 16
16 libdispatch.dylib              0x184936200 _dispatch_block_invoke_direct$VARIANT$mp + 288
17 libdispatch.dylib              0x1849360ac dispatch_block_perform$VARIANT$mp + 104
18 Foundation                     0x185987878 __NSOQSchedule_f + 376
19 libdispatch.dylib              0x18492ea14 _dispatch_client_callout + 16
20 libdispatch.dylib              0x184936f08 _dispatch_continuation_pop$VARIANT$mp + 428
21 libdispatch.dylib              0x18493580c _dispatch_async_redirect_invoke$VARIANT$mp + 604
22 libdispatch.dylib              0x18493bcf4 _dispatch_root_queue_drain + 600
23 libdispatch.dylib              0x18493ba38 _dispatch_worker_thread3 + 120
24 libsystem_pthread.dylib        0x184bd706c _pthread_wqthread + 1268
25 libsystem_pthread.dylib        0x184bd6b6c start_wqthread + 4

===> And code of me. It is line 304

    navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:highLightColour, NSFontAttributeName:font]

Update: I found the cause in my code. It runs two threads at the same time.

Example:

let queue: OperationQueue = OperationQueue()
        queue.maxConcurrentOperationCount = 2
        queue.addOperation({
            let navigationBarAppearace = UINavigationBar.appearance()
            navigationBarAppearace.barTintColor =  UIColor.red
            navigationBarAppearace.tintColor = UIColor.yellow
            if let font = UIFont(name: "Lato-Regular", size: 18.0) {
                navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.blue, NSFontAttributeName:font]
            }

        })
        queue.addOperation({
           let navigationBarAppearace = UINavigationBar.appearance()
            navigationBarAppearace.barTintColor =  UIColor.red
            navigationBarAppearace.tintColor = UIColor.yellow
            if let font = UIFont(name: "Lato-Regular", size: 18.0) {
                navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.blue, NSFontAttributeName:font]
            }  
        })

However, I do not understand why running with two threads makes it error. Do you have a reason why?

Upvotes: 2

Views: 241

Answers (1)

Shamim Hossain
Shamim Hossain

Reputation: 1760

When interacting with Apples UI frameworks, we need to ensure that calls makes in main thread because iOS updates UI on main thread only. Accessing single resource from multiple thread creates deadlock situation.

However, you should switch to main queue to update UI related works.

    // back to UI thread
    OperationQueue.main.addOperation {
        let navigationBarAppearace = UINavigationBar.appearance()
        navigationBarAppearace.barTintColor =  UIColor.red
        navigationBarAppearace.tintColor = UIColor.yellow
        if let font = UIFont(name: "Lato-Regular", size: 18.0) {
            navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.blue, NSFontAttributeName:font]
        }
    }

You can read this article for Threading on Swift

Thanks!

Upvotes: 1

Related Questions