修改导航栏外观时与 NSAppearance 相关的 NSInternalInconsistencyException

NSInternalInconsistencyException related to NSAppearance while modifying a navigation bar appearance

我有问题。你能告诉我如何解决它吗?谢谢 请查看崩溃,如果您能找到其背后的原因,请提供帮助。

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

===> 还有我的代码。是第304行

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

更新:我在我的代码中找到了原因。它同时运行两个线程。

示例:

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]
            }  
        })

但是,我不明白为什么运行两个线程会报错。你有理由吗?

与 Apples UI 框架交互时,我们需要确保调用在主线程中进行,因为 iOS 仅在主线程中更新 UI。从多个线程访问单个资源会造成死锁情况。

但是,您应该切换到主队列来更新UI相关作品。

    // 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]
        }
    }

您可以阅读这篇文章 Threading on Swift

谢谢!