***由于未捕获的异常"NSInternalInconsistencyException"而终止应用程序,原因:“线程违规:预期为主线程" [英] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'threading violation: expected the main thread'

查看:144
本文介绍了***由于未捕获的异常"NSInternalInconsistencyException"而终止应用程序,原因:“线程违规:预期为主线程"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,当用户点击个人资料图片将其照片上传到应用程序时,我们的应用程序崩溃.该应用程序向用户发送授权请求,一旦点击允许"按钮,此崩溃立即发生.

I'm having an issue where our application is crashing when the user taps the profile picture to upload their photo to the app. The app sends an authorization request to the user, and this crash happens immediantly once the "allow" button is tapped.

也许相关信息是这不是根视图控制器.注册过程在同一情节提要板上具有一系列页面.在需要我不是主根控制器的权限的任何页面上都会发生这种情况.

Maybe relevant information is that this is not the root view controller. The registration process has a series of pages on the same Storyboard. This is occuring on any page where I need to request permissions that isn't the main root controller.

我试图将PhotoAuthorization代码块放入DispatchQueue.main.async中,但这似乎不起作用.这是前开发人员的遗留代码,因此我仍在努力修复一些问题.

I have attempted to place the PhotoAuthorization block of code into a DispatchQueue.main.async, but that didn't seem to work. This is legacy code by a former developer, so I'm still working on fixing up some stuff.

涉嫌导致应用程序崩溃的代码块:

Code block suspected of crashing the app:

    func checkPermission() {
        let photoAuthStatus = PHPhotoLibrary.authorizationStatus()
        switch photoAuthStatus {
        case .authorized:
            self.showPhotoActionSheet()
        case .notDetermined:
            PHPhotoLibrary.requestAuthorization({ (newStatus) in
                if newStatus == PHAuthorizationStatus.authorized {
                    self.showPhotoActionSheet()
                }
            })
        case .restricted:
            showPermissionRequestReason()
        case .denied:
            showPermissionRequestReason()
        }
    }

    func showPhotoActionSheet() {
        let actionSheet = YoutubeActionController()
        actionSheet.addAction(Action(ActionData(title: "Take Photo", image: UIImage(named: "ic_photo_camera")!), style: .default, handler: { action in 
            if UIImagePickerController.isSourceTypeAvailable(.camera) {
                self.imagePicker.sourceType = .camera
                self.imagePicker.allowsEditing = true
                self.present(self.imagePicker, animated: true, completion: nil)
            }
        }))
        actionSheet.addAction(Action(ActionData(title: "Choose from Camera Roll", image: UIImage(named: "ic_photo_album")!), style: .default, handler: { action in 
            if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum) {
                self.imagePicker.sourceType = .savedPhotosAlbum
                self.imagePicker.allowsEditing = true
                self.present(self.imagePicker, animated: true, completion: nil)
            }
        }))
        actionSheet.addAction(Action(ActionData(title: "Cancel", image: UIImage(named: "ic_cancel")!), style: .cancel, handler: nil))
        present(actionSheet, animated: true, completion: nil)
    }

在下面找到回溯.任何帮助是极大的赞赏!

Find the traceback below. Any help is greatly appreciated!

2020-01-23 13:39:23.878772+0000 ***[89799:17598462] *** Assertion failure in -[FBSSerialQueue assertOnQueue], /BuildRoot/Library/Caches/com.apple.xbs/Sources/FrontBoardServices_Sim/FrontBoard-626.2/FrontBoardServices/FBSSerialQueue.m:98
2020-01-23 13:39:23.990535+0000 ***[89799:17598462] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'threading violation: expected the main thread'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000115f7102e __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x0000000115ddeb20 objc_exception_throw + 48
    2   CoreFoundation                      0x0000000115f70da8 +[NSException raise:format:arguments:] + 88
    3   Foundation                          0x000000010f613b61 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
    4   FrontBoardServices                  0x000000011c344a8b -[FBSSerialQueue assertOnQueue] + 236
    5   FrontBoardServices                  0x000000011c2f77b9 -[FBSSceneImpl updateClientSettings:withTransitionContext:] + 70
    6   FrontBoardServices                  0x000000011c2f7a04 -[FBSSceneImpl updateClientSettingsWithTransitionBlock:] + 154
    7   FrontBoardServices                  0x000000011c2f7929 -[FBSSceneImpl updateClientSettingsWithBlock:] + 110
    8   UIKitCore                           0x00000001200c7aa0 -[FBSScene(UIApp) updateUIClientSettingsWithBlock:] + 160
    9   UIKitCore                           0x000000011fcbf5f6 -[_UISystemAppearanceManager updateScreenEdgesDeferringSystemGestures] + 374
    10  UIKitCore                           0x000000011ff229e2 __70-[UIViewController setNeedsUpdateOfScreenEdgesDeferringSystemGestures]_block_invoke_2 + 118
    11  UIKitCore                           0x000000011ff00cb5 -[UIViewController _updateSystemAppearanceWithRecursionBlock:action:] + 295
    12  UIKitCore                           0x000000011ff22629 -[UIViewController _setPresentedStatusBarViewController:] + 220
    13  UIKitCore                           0x000000011ff129c3 -[UIViewController _presentViewController:modalSourceViewController:presentationController:animationController:interactionController:completion:] + 1381
    14  UIKitCore                           0x000000011ff143c6 -[UIViewController _presentViewController:withAnimationController:completion:] + 4349
    15  UIKitCore                           0x000000011ff16c47 __63-[UIViewController _presentViewController:animated:completion:]_block_invoke + 98
    16  UIKitCore                           0x000000011ff1715f -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 511
    17  UIKitCore                           0x000000011ff16ba5 -[UIViewController _presentViewController:animated:completion:] + 187
    18  UIKitCore                           0x000000011ff16e0c -[UIViewController presentViewController:animated:completion:] + 150
    19  ***                      0x000000010dc472ca $s14***26RegistrationViewControllerC20showPhotoActionSheetyyF + 2522
    20  ***                      0x000000010dc462b9 $s14***26RegistrationViewControllerC15checkPermissionyyFySo21PHAuthorizationStatusVcfU_ + 201
    21  ***                      0x000000010dc46355 $sSo21PHAuthorizationStatusVIegy_ABIeyBy_TR + 53
    22  Photos                              0x0000000116a72643 __39+[PHPhotoLibrary requestAuthorization:]_block_invoke + 52
    23  AssetsLibraryServices               0x000000013927ff7e __79-[PLPrivacy _isPhotosAccessAllowedWithScope:forceHandler:accessAllowedHandler:]_block_invoke.14 + 501
    24  AssetsLibraryServices               0x000000013924b60c __pl_dispatch_async_block_invoke + 25
    25  libdispatch.dylib                   0x0000000117a02848 _dispatch_call_block_and_release + 12
    26  libdispatch.dylib                   0x0000000117a037b9 _dispatch_client_callout + 8
    27  libdispatch.dylib                   0x0000000117a09526 _dispatch_lane_serial_drain + 707
    28  libdispatch.dylib                   0x0000000117a09f5c _dispatch_lane_invoke + 388
    29  libdispatch.dylib                   0x0000000117a13ff9 _dispatch_workloop_worker_thread + 626
    30  libsystem_pthread.dylib             0x00007fff51bfd611 _pthread_wqthread + 421
    31  libsystem_pthread.dylib             0x00007fff51bfd3fd start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException

推荐答案

该错误告诉您要执行的操作:

The error tells you want to do:

原因:线程违规:预期为主线程"

reason: 'threading violation: expected the main thread'

很多时候,当您通过闭包时,您可能会在后台线程上被调用回去.但是,如果您想对UI进行任何操作,则必须在主线程上完成.我们使用 DispatchQueue.main.async 将一个块传递到主队列,并使其异步在主线程上运行,

Many times, when you pass a closure, you could be called back on a background thread. But, if you want to do anything with the UI, that must be done on the main thread. We use DispatchQueue.main.async to pass a block to the main queue and have it run on the main thread asynchronously,

PHPhotoLibrary.requestAuthorization({ (newStatus) in
    if newStatus == PHAuthorizationStatus.authorized {
        self.showPhotoActionSheet()
    }
})    

您需要分派到主线程

PHPhotoLibrary.requestAuthorization({ (newStatus) in
    if newStatus == PHAuthorizationStatus.authorized {
        DispatchQueue.main.async {
            self.showPhotoActionSheet()
        }

    }
})    

似乎您可能在后台调用checkPermission.如果是这样,您还需要将调用包装到:

It looks like you might be calling checkPermission on the background. If so, you also need to wrap the call to:

self.showPhotoActionSheet()

在调用堆栈中-看来这可能是您遇到问题的那个.

From the call stack -- it looks like this might be the one you are having problems with.

这篇关于***由于未捕获的异常"NSInternalInconsistencyException"而终止应用程序,原因:“线程违规:预期为主线程"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆