找不到 "This application is modifying the autolayout engine from a background thread" 的来源

Can not find origin of "This application is modifying the autolayout engine from a background thread"

我的应用程序仅在我第一次登录时崩溃。它使用了 GoogleMaps (3.7.0) pod,我怀疑错误来自该库:

 Stack:(
    0   Foundation                          0x000000018c1b8550 80C31587-F538-3437-966F-300F6632EB95 + 2331984
    1   Foundation                          0x000000018bfb18c4 80C31587-F538-3437-966F-300F6632EB95 + 207044
    2   Foundation                          0x000000018bfb17d8 80C31587-F538-3437-966F-300F6632EB95 + 206808
    3   Foundation                          0x000000018bfb1448 80C31587-F538-3437-966F-300F6632EB95 + 205896
    4   UIKitCore                           0x000000019025a824 AA897CA9-8D15-3DD7-BB4F-8D90F9A28571 + 15611940
    5   UIKitCore                           0x000000019026db1c AA897CA9-8D15-3DD7-BB4F-8D90F9A28571 + 15690524
    6   libobjc.A.dylib                     0x000000018b9e0a4c A486C2B3-41B0-3F23-AF40-C5579F5BE97E + 6732
    7   QuartzCore                          0x0000000192883a9c A3363F67-6783-3979-8FA9-5563ABD4737D + 1403548
    8   QuartzCore                          0x0000000192883eec A3363F67-6783-3979-8FA9-5563ABD4737D + 1404652
    9   QuartzCore                          0x0000000192896614 A3363F67-6783-3979-8FA9-5563ABD4737D + 1480212
    10  QuartzCore                          0x00000001927db1c4 A3363F67-6783-3979-8FA9-5563ABD4737D + 713156
    11  QuartzCore                          0x0000000192805fd0 A3363F67-6783-3979-8FA9-5563ABD4737D + 888784
    12  QuartzCore                          0x0000000192806fb8 A3363F67-6783-3979-8FA9-5563ABD4737D + 892856
    13  libsystem_pthread.dylib             0x000000018b9dd104 5F848A4C-4907-3FC9-A76D-378FE8F2E4FC + 37124
    14  libsystem_pthread.dylib             0x000000018b9d58a0 5F848A4C-4907-3FC9-A76D-378FE8F2E4FC + 6304
    15  libsystem_pthread.dylib             0x000000018b9d7a20 5F848A4C-4907-3FC9-A76D-378FE8F2E4FC + 14880
    16  libsystem_pthread.dylib             0x000000018b9d7760 _pthread_wqthread + 424
    17  libsystem_pthread.dylib             0x000000018b9dd9e8 start_wqthread + 8
)

这是显示的跟踪日志 XCode。

编辑截图:

这就是我从 ViewModel 接收回调的方式。我在所有输出中添加了 DispatchQueue.main 以尝试避免此异常,但仍然弹出....

disposeBag.insert(
            viewModel.outputs.onShowLoading
                .bind { show in
                    DispatchQueue.main.async { 
                        self.showLoading(show: show)
                    }
                },

            viewModel.outputs.onLoadDone
                .bind { mapInfo in
                    DispatchQueue.main.async {
                        self.loadMap(mapInfo: mapInfo)
                    }
                },

            viewModel.outputs.onLoadError
                .bind { error in
                    DispatchQueue.main.async {
                        print("ON LOAD ASSETS ERROR: \(error.localizedDescription)")
                    }
                },

            viewModel.outputs.onMultiClusterTap
                .bind { paths in
                    DispatchQueue.main.async {
                        self.centerMap(paths: paths)
                    }
                },

            viewModel.outputs.onLoadAssetsInfoDone
                .bind { assets in
                    DispatchQueue.main.async {
                        self.showBottomSheet(assets: assets)
                    }
                },

            viewModel.outputs.onDeviceChanged
                .bind {
                    DispatchQueue.main.async {
                        self!.showDeviceChangedError()
                    }
                }
        )

编辑 (02/05/2020): 我尝试使用 NBUIKitMainThreadGuard

查找错误

但我仍然找不到这些错误的根源。 另外,我发现在注销-登录后也会出现这个错误。这不仅仅是我第一次安装应用程序。

编辑 (07/05/2020): 也许正如 Werner Altewischer 指出的那样,有一个图形库正在解决这个问题。这是我的播客文件,也许有助于解决这个问题:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '11.0'
use_frameworks!

target 'WatchmanDoor' do

    pod 'DropDown', '2.3.13'
    pod 'GoogleMaps', '3.7.0'
    pod 'GooglePlaces'
    pod 'DKImagePickerController', '4.2.1'
    pod 'Alamofire', '4.9.1'
    pod 'iOSDFULibrary', '4.6.1'
    pod 'RxSwift', '~> 5.0.1'
    pod 'RxCocoa', '~> 5.0.1'
    pod 'Swinject', '~> 2.7.1'
    pod 'SwinjectAutoregistration', '~> 2.7.0'
    pod 'SwinjectStoryboard', '~> 2.2.0'
    pod 'FittedSheets', :git => 'https://github.com/WatchmanDoor/FittedSheets.git', :commit => '9824dd171499967226a4dd160aedc7d4d9db3a11'
    pod 'SDWebImage', '~> 5.5.0'
    pod 'NavigationDrawer', '1.0.2'

end

难道是 FittedSheets pod 引起了错误?我 fork 存储库以更新它以支持 Swift 5,但可能是导致异常的原因。

更新: 仍然面临同样的错误...找不到其他任何东西 -_- 知道从哪里开始吗?

完整崩溃报告:

Incident Identifier: 34A039F6-9E4A-4E5C-B9B4-AB531A8E8D93
CrashReporter Key:   5f326dac824b350bd2afaacbdb938d680de621bc
Hardware Model:      iPhone8,4
Process:             WatchmanDoor [476]
Path:                /private/var/containers/Bundle/Application/717E7E5E-4E42-4950-B8C8-FC2042C09EDC/WatchmanDoor.app/WatchmanDoor
Identifier:          com.stmseguridad.WatchmanDoor
Version:             1 (2.0.9)
Code Type:           ARM-64 (Native)
Role:                Foreground
Parent Process:      launchd [1]
Coalition:           com.stmseguridad.WatchmanDoor [625]


Date/Time:           2020-04-16 14:21:00.9449 +0200
Launch Time:         2020-04-16 14:18:12.7530 +0200
OS Version:          iPhone OS 13.4 (17E255)
Release Type:        User
Baseband Version:    9.52.01
Report Version:      104

Exception Type:  EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x00000001a9d2d6e0
Termination Signal: Trace/BPT trap: 5
Termination Reason: Namespace SIGNAL, Code 0x5
Terminating Process: exc handler [476]
Triggered by Thread:  3

Last Exception Backtrace:
0   CoreFoundation                  0x1aa0cc5f0 __exceptionPreprocess + 224
1   libobjc.A.dylib                 0x1a9deebcc objc_exception_throw + 55
2   Foundation                      0x1aa5a3dac _AssertAutolayoutOnAllowedThreadsOnly + 419
3   Foundation                      0x1aa3b0c2c -[NSISEngine _optimizeWithoutRebuilding] + 67
4   Foundation                      0x1aa3b0b48 -[NSISEngine optimize] + 111
5   Foundation                      0x1aa3b07bc -[NSISEngine performPendingChangeNotifications] + 111
6   UIKitCore                       0x1ae6132d0 -[UIView+ 15499984 (Hierarchy) layoutSubviews] + 307
7   UIKitCore                       0x1ae625c7c -[UIView+ 15576188 (CALayerDelegate) layoutSublayersOfLayer:] + 2143
8   QuartzCore                      0x1b0bbf4ac -[CALayer layoutSublayers] + 283
9   QuartzCore                      0x1b0bc5604 CA::Layer::layout_if_needed+ 1406468 (CA::Transaction*) + 467
10  QuartzCore                      0x1b0bd0148 CA::Layer::layout_and_display_if_needed+ 1450312 (CA::Transaction*) + 139
11  QuartzCore                      0x1b0b18e34 CA::Context::commit_transaction+ 699956 (CA::Transaction*, double) + 295
12  QuartzCore                      0x1b0b427c4 CA::Transaction::commit+ 870340 () + 675
13  QuartzCore                      0x1b0b43764 CA::Transaction::release_thread+ 874340 (void*) + 227
14  libsystem_pthread.dylib         0x1a9de5f8c _pthread_tsd_cleanup + 579
15  libsystem_pthread.dylib         0x1a9de2df4 _pthread_exit + 79
16  libsystem_pthread.dylib         0x1a9de3e64 _pthread_wqthread_exit + 95
17  libsystem_pthread.dylib         0x1a9de3c04 _pthread_wqthread + 415
18  libsystem_pthread.dylib         0x1a9de6740 start_wqthread + 7


Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0:
0   libobjc.A.dylib                 0x00000001a9deab4c objc_msgSend + 44
1   AppleMetalGLRenderer            0x00000001c4b60624 GLDContextRec::setRenderState+ 54820 (unsigned int) + 84
2   AppleMetalGLRenderer            0x00000001c4b67084 gldRenderVertexArray+ 82052 (GLDContextRec*, unsigned int, unsigned int, int, int, unsigned int, void const*, int, void const*) + 1108
3   GLEngine                        0x00000001c4ae3cd0 glDrawElements_ACC_ES2Exec + 416
4   WatchmanDoor                    0x0000000103a12fcc 0x102c50000 + 14430156
5   WatchmanDoor                    0x0000000103a17be0 0x102c50000 + 14449632
6   WatchmanDoor                    0x0000000103ad4bb0 0x102c50000 + 15223728
7   WatchmanDoor                    0x0000000103a1088c 0x102c50000 + 14420108
8   WatchmanDoor                    0x0000000103a9ece8 0x102c50000 + 15002856
9   WatchmanDoor                    0x0000000103a82788 0x102c50000 + 14886792
10  WatchmanDoor                    0x0000000103acfc5c 0x102c50000 + 15203420
11  WatchmanDoor                    0x0000000103a80da8 0x102c50000 + 14880168
12  WatchmanDoor                    0x0000000103a7fc74 0x102c50000 + 14875764
13  QuartzCore                      0x00000001b0a7f368 CA::Display::DisplayLink::dispatch_items+ 70504 (unsigned long long, unsigned long long, unsigned long long) + 516
14  IOKit                           0x00000001ab010628 IODispatchCalloutFromCFMessage + 480
15  CoreFoundation                  0x00000001aa021bc0 __CFMachPortPerform + 172
16  CoreFoundation                  0x00000001aa04b200 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 56
17  CoreFoundation                  0x00000001aa04a90c __CFRunLoopDoSource1 + 444
18  CoreFoundation                  0x00000001aa0456c0 __CFRunLoopRun + 1888
19  CoreFoundation                  0x00000001aa044c34 CFRunLoopRunSpecific + 424
20  GraphicsServices                0x00000001b418e38c GSEventRunModal + 160
21  UIKitCore                       0x00000001ae17722c UIApplicationMain + 1932
22  WatchmanDoor                    0x000000010360fa9c 0x102c50000 + 10222236
23  libdyld.dylib                   0x00000001a9ecc800 start + 4

Thread 1 name:  com.apple.uikit.eventfetch-thread
Thread 1:
0   libsystem_kernel.dylib          0x00000001a9ea0198 mach_msg_trap + 8
1   libsystem_kernel.dylib          0x00000001a9e9f60c mach_msg + 72
2   CoreFoundation                  0x00000001aa04a3b4 __CFRunLoopServiceMachPort + 148
3   CoreFoundation                  0x00000001aa0453e8 __CFRunLoopRun + 1160
4   CoreFoundation                  0x00000001aa044c34 CFRunLoopRunSpecific + 424
5   Foundation                      0x00000001aa387bcc -[NSRunLoop+ 31692 (NSRunLoop) runMode:beforeDate:] + 228
6   Foundation                      0x00000001aa387aac -[NSRunLoop+ 31404 (NSRunLoop) runUntilDate:] + 88
7   UIKitCore                       0x00000001ae219160 -[UIEventFetcher threadMain] + 152
8   Foundation                      0x00000001aa4b69d0 __NSThread__start__ + 848
9   libsystem_pthread.dylib         0x00000001a9de2d98 _pthread_start + 156
10  libsystem_pthread.dylib         0x00000001a9de674c thread_start + 8

Thread 2:
0   libsystem_pthread.dylib         0x00000001a9de6738 start_wqthread + 0

Thread 3 Crashed:
0   libsystem_c.dylib               0x00000001a9d2d6e0 __abort + 144
1   libsystem_c.dylib               0x00000001a9d2d6e0 __abort + 144
2   libsystem_c.dylib               0x00000001a9d2d650 __abort + 0
3   libc++abi.dylib                 0x00000001a9e95cc0 __cxxabiv1::__aligned_malloc_with_fallback+ 76992 (unsigned long) + 0
4   libc++abi.dylib                 0x00000001a9e87e10 demangling_unexpected_handler+ 19984 () + 0
5   libobjc.A.dylib                 0x00000001a9deee80 _objc_terminate+ 24192 () + 124
6   libc++abi.dylib                 0x00000001a9e9514c std::__terminate(void (*)+ 74060 ()) + 16
7   libc++abi.dylib                 0x00000001a9e97bd8 __cxa_get_exception_ptr + 0
8   libc++abi.dylib                 0x00000001a9e97b98 __cxxabiv1::exception_cleanup_func+ 84888 (_Unwind_Reason_Code, _Unwind_Exception*) + 0
9   libobjc.A.dylib                 0x00000001a9deecf8 _objc_exception_destructor+ 23800 (void*) + 0
10  Foundation                      0x00000001aa5a3dac -[NSISEngine tryToOptimizeReturningMutuallyExclusiveConstraints] + 0
11  Foundation                      0x00000001aa3b0c2c -[NSISEngine _optimizeWithoutRebuilding] + 68
12  Foundation                      0x00000001aa3b0b48 -[NSISEngine optimize] + 112
13  Foundation                      0x00000001aa3b07bc -[NSISEngine performPendingChangeNotifications] + 112
14  UIKitCore                       0x00000001ae6132d0 -[UIView+ 15499984 (Hierarchy) layoutSubviews] + 308
15  UIKitCore                       0x00000001ae625c7c -[UIView+ 15576188 (CALayerDelegate) layoutSublayersOfLayer:] + 2144
16  QuartzCore                      0x00000001b0bbf4ac -[CALayer layoutSublayers] + 284
17  QuartzCore                      0x00000001b0bc5604 CA::Layer::layout_if_needed+ 1406468 (CA::Transaction*) + 468
18  QuartzCore                      0x00000001b0bd0148 CA::Layer::layout_and_display_if_needed+ 1450312 (CA::Transaction*) + 140
19  QuartzCore                      0x00000001b0b18e34 CA::Context::commit_transaction+ 699956 (CA::Transaction*, double) + 296
20  QuartzCore                      0x00000001b0b427c4 CA::Transaction::commit+ 870340 () + 676
21  QuartzCore                      0x00000001b0b43764 CA::Transaction::release_thread+ 874340 (void*) + 228
22  libsystem_pthread.dylib         0x00000001a9de5f8c _pthread_tsd_cleanup + 580
23  libsystem_pthread.dylib         0x00000001a9de2df4 _pthread_exit + 80
24  libsystem_pthread.dylib         0x00000001a9de3e64 _pthread_wqthread_legacy_worker_wrap + 0
25  libsystem_pthread.dylib         0x00000001a9de3c04 _pthread_wqthread + 416
26  libsystem_pthread.dylib         0x00000001a9de6740 start_wqthread + 8

Thread 4 name:  com.apple.NSURLConnectionLoader
Thread 4:
0   libsystem_kernel.dylib          0x00000001a9ea0198 mach_msg_trap + 8
1   libsystem_kernel.dylib          0x00000001a9e9f60c mach_msg + 72
2   CoreFoundation                  0x00000001aa04a3b4 __CFRunLoopServiceMachPort + 148
3   CoreFoundation                  0x00000001aa0453e8 __CFRunLoopRun + 1160
4   CoreFoundation                  0x00000001aa044c34 CFRunLoopRunSpecific + 424
5   CFNetwork                       0x00000001ad309c44 0x1ad308000 + 7236
6   Foundation                      0x00000001aa4b69d0 __NSThread__start__ + 848
7   libsystem_pthread.dylib         0x00000001a9de2d98 _pthread_start + 156
8   libsystem_pthread.dylib         0x00000001a9de674c thread_start + 8

Thread 5 name:  com.google.Maps.LabelingBehavior
Thread 5:
0   libsystem_kernel.dylib          0x00000001a9ea0198 mach_msg_trap + 8
1   libsystem_kernel.dylib          0x00000001a9e9f60c mach_msg + 72
2   CoreFoundation                  0x00000001aa04a3b4 __CFRunLoopServiceMachPort + 148
3   CoreFoundation                  0x00000001aa0453e8 __CFRunLoopRun + 1160
4   CoreFoundation                  0x00000001aa044c34 CFRunLoopRunSpecific + 424
5   Foundation                      0x00000001aa387bcc -[NSRunLoop+ 31692 (NSRunLoop) runMode:beforeDate:] + 228
6   WatchmanDoor                    0x0000000103b4e330 0x102c50000 + 15721264
7   Foundation                      0x00000001aa4b69d0 __NSThread__start__ + 848
8   libsystem_pthread.dylib         0x00000001a9de2d98 _pthread_start + 156
9   libsystem_pthread.dylib         0x00000001a9de674c thread_start + 8

Thread 6:
0   libsystem_pthread.dylib         0x00000001a9de6738 start_wqthread + 0

Thread 7:
0   libsystem_pthread.dylib         0x00000001a9de6738 start_wqthread + 0

Thread 8:
0   libsystem_pthread.dylib         0x00000001a9de6738 start_wqthread + 0

Thread 3 crashed with ARM Thread State (64-bit):
    x0: 0x0000000000000000   x1: 0x0000000000000000   x2: 0x0000000000000000   x3: 0x0000000000000001
    x4: 0x0000000000000000   x5: 0x0000000000989680   x6: 0x000000000000006e   x7: 0x0000000000000001
    x8: 0x0000000000000000   x9: 0x0000000000000002  x10: 0x000000002daafd89  x11: 0x000000000000000b
   x12: 0x00000001e48ea080  x13: 0x0000000000000000  x14: 0x0000000000000010  x15: 0x0000000000000000
   x16: 0x0000000000000030  x17: 0x00000002d7ffffff  x18: 0x0000000000000000  x19: 0x000000016d57ec48
   x20: 0x00000001f3404c80  x21: 0x000000016d57ed00  x22: 0x0000000169ba05b0  x23: 0x00000001e4b8c000
   x24: 0x0000000000000001  x25: 0x0000000000000068  x26: 0x000000000000009c  x27: 0x0000000000000068
   x28: 0x000000002b310019   fp: 0x000000016d57ec60   lr: 0x00000001a9d2d6e0
    sp: 0x000000016d57ec30   pc: 0x00000001a9d2d6e0 cpsr: 0x80000000
   esr: 0xf2000001  Address size fault

重要更新: 我发现,当应用程序崩溃时,我的堆栈视图充满了 UITRANSITIONVIEW。不知道这个有没有关系...

解决方案 [13/05/2020] 所以我想我找到了错误的来源。请参阅下面的答案。

显然,有问题的后台线程(线程 17)正在直接访问某些 UIKit 方法,这通常是不允许的(只能从主线程完成)。

您是否使用 GCD 的分派异步在后台线程上分派了一些方法?

所以我想我找到了错误的来源。 当我收到 API 的资产列表响应时,我创建了一个 MapInfoModel,如下所示:

private func generateMapInfo(assets: [APIAssetModel]) -> MapInfoModel {
    var markers = [ClusterMarkerItem]()
    var locations = [CLLocationCoordinate2D]()
    assets.forEach({ asset in
        if nil != asset.lat && nil != asset.lng {
            if asset.lat! > -85 && asset.lat! < 85 {
                let latlng = CLLocationCoordinate2D(latitude: asset.lat!, longitude: asset.lng!)
                for index in 0..<(asset.productsCount ?? 0) {
                    let marker = GMSMarker(position: latlng)
                    marker.title = "\(asset.id)_\(index)"
                    markers.append(ClusterMarkerItem(position: latlng,
                                                     marker: marker,
                                                     asset: asset,
                                                     prod_position: index))
                }
                locations.append(latlng)
            }
        }
    })
    return MapInfoModel(assetList: assets, clusterList: markers, locationList: locations)
}

此方法是从后台线程调用的,如您所见,我正在实例化 GMSMarker。这不会增加任何 error/warning 并且地图已加载并很好地显示了集群。但是,事情是这样的,我怀疑在加载集群后,Google Maps SDK 对其中加载的 GMSMarkers 做了一些事情,我怀疑,该操作是从创建 GMSMarker 的线程调用的,所以,如果它是从后台线程创建的,XCode 将显示错误:

This application is modifying the autolayout engine from a background thread

我只是猜测,但我正在等待 Google 确认。所以解决方案是从主线程实例化 GMSMarker(在这种情况下我使用 DispatchQueue.main.async{})。

更新 Google 给我发邮件确认一下。他们是这样说的:

Our specialists have confirmed that all Marker creation and updates should be happening on the main thread.

This is because all drawing and rendering in iOS has to happen on the main thread, and updates done to the markers translates into that directly.

We will be working on including this information in our official documentation for future references.