简体   繁体   English

仅在主线程上运行异常

[英]Only run on the main thread exception

I am tracking crashes in my app. 我正在跟踪应用程序中的崩溃。 This particular crash gives me the error "Terminating app due to an uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!'". 这种特殊的崩溃给我错误“由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'仅在主线程上运行!'”。 The weird thing is i can't seem to recreate the crash using my device or simulator but in my analytics it shows that this crash happens a lot. 奇怪的是,我似乎无法使用设备或模拟器来重新创建崩溃,但是在我的分析中,它表明该崩溃经常发生。 I was told this was happening because you can only do UI updates on the main thread so i updated my code but this crash keeps happening and i still can't recreate the crash on my phone or simulator. 有人告诉我这是因为您只能在主线程上进行UI更新,所以我更新了代码,但是此崩溃不断发生,而我仍然无法在手机或模拟器上重新创建崩溃。

This is code where the crash is happening: 这是发生崩溃的代码:

indicator.startAnimation()
let audioData = NSData(contentsOfURL: EditSongViewController.url.urlComplete!)
let dataImage: NSData = UIImageJPEGRepresentation(songImage.image!, 1.0)!

let imageFile = PFFile(name: "photo.jpg", data: dataImage)
let audioFile = PFFile(name: "song.mp4", data: audioData)
audioFile.saveInBackground()
imageFile.saveInBackground()

let testObject = PFObject(className: "PrivateSongs")
testObject["songFile"] = audioFile
testObject["image"] = imageFile
testObject["username"] = PFUser.currentUser().username
testObject["user"] = PFUser.currentUser()
testObject["title"] = self.songTitleText.text
testObject["songInfo"] = self.songInfoText.text
if RecordViewController.fileURL.songLyrics != nil {
    testObject["lyrics"] = RecordViewController.fileURL.songLyrics
}
if (isPrivate == true) {
    testObject["isPrivate"] = true
} else {
    testObject["isPrivate"] = false
}

testObject.saveInBackgroundWithBlock ({ (success, error) -> Void in
    if (success) {

        if self.chartsSwitch.on {
            let chart = PFObject(className: "Songs")
            chart["artistName"] = PFUser.currentUser().username
            chart["songFile"] = audioFile
            chart["title"] = self.songTitleText.text
            chart["picture"] = imageFile
            chart["user"] = PFUser.currentUser()
            chart["likes"] = 0
            if RecordViewController.fileURL.songLyrics != nil {
                chart["lyrics"] = RecordViewController.fileURL.songLyrics
            }
            chart.saveInBackground()

        }
        PostViewControler.share.shareUrl = audioFile.url!
        dispatch_async(dispatch_get_main_queue(), ^{
        /* Your UI code */
        self.postButton.hidden = true
        self.addPictureButton.hidden = true
        self.indicator.stopAnimation(false, completion: nil)

          self.navigationController!.pushViewController(self.storyboard!.instantiateViewControllerWithIdentifier("ShareViewController") , animated: true)     
        });

    } else {
        dispatch_async(dispatch_get_main_queue(), ^{
        /* Your UI code */
        self.indicator.stopAnimation(false, completion: nil)
        self.helper.showErrorAlert("Couldn't save your song please try again.")
        });

    }
})

Crash: 崩溃:

Terminating app due to an uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!' Error symbolicating crash report.
Last Exception Backtrace
0   CoreFoundation  0x2409068b  
1   libobjc.A.dylib 0x3558ee17  objc_exception_throw + 38
2   CoreFoundation  0x24090561  <redacted> + 0
3   Foundation  0x24e1b0e3  <redacted> + 86
4   UIFoundation    0x323d5a6f  <redacted> + 398
5   UIFoundation    0x323d57b3  <redacted> + 1510
6   UIFoundation    0x32404989  <redacted> + 724
7   UIFoundation    0x32404a7d  <redacted> + 52
8   UIFoundation    0x324277a3  <redacted> + 122
9   UIFoundation    0x32427339  <redacted> + 400
10  UIFoundation    0x32426f2b  <redacted> + 78
11  UIKit   0x28203227  <redacted> + 262
12  UIKit   0x28203107  <redacted> + 178
13  meipmusic   0x145e74    meipmusic + 437876
14  meipmusic   0x146e20    meipmusic + 441888
15  meipmusic   0x147144    meipmusic + 442692
16  meipmusic   0x13c994    meipmusic + 399764
17  meipmusic   0x14d760    meipmusic + 468832
18  meipmusic   0x19d810    meipmusic + 796688
19  AVFoundation    0x228625e7  <redacted> + 558
20  libdispatch.dylib   0x35c90d2b  <redacted> + 10
21  libdispatch.dylib   0x35c9d92b  <redacted> + 1758
22  libdispatch.dylib   0x35c9cf69  <redacted> + 284
23  libdispatch.dylib   0x35c9f665  <redacted> + 396
24  libdispatch.dylib   0x35c9f4d5  <redacted> + 96
25  libsystem_pthread.dylib 0x35e4ab45  _pthread_wqthread + 1024
26  libsystem_pthread.dylib 0x35e4a734  start_wqthread + 8
Crashed Thread 20 :
0   CoreFoundation  0x240909c7  
1   libobjc.A.dylib 0x3558f09f  
2   libc++abi.dylib 0x34c63e17  
3   libc++abi.dylib 0x34c638f9  
4   libobjc.A.dylib 0x3558ef5f  
5   AVFoundation    0x22862651  
6   libdispatch.dylib   0x35c90d2b  <redacted> + 10
7   libdispatch.dylib   0x35c9d92b  <redacted> + 1758
8   libdispatch.dylib   0x35c9cf69  <redacted> + 284
9   libdispatch.dylib   0x35c9f665  <redacted> + 396
10  libdispatch.dylib   0x35c9f4d5  <redacted> + 96
11  libsystem_pthread.dylib 0x35e4ab45  _pthread_wqthread + 1024
12  libsystem_pthread.dylib 0x35e4a734  start_wqthread + 8
Thread 0 :
0   libsystem_kernel.dylib  0x35d96164  
1   CoreFoundation  0x24053905  
2   CoreFoundation  0x24051c8d  
3   CoreFoundation  0x23fa5249  
4   CoreFoundation  0x23fa5035  
5   GraphicsServices    0x2d058ad1  
6   UIKit   0x281ba899  
7   meipmusic   0x1849f0    
8   libdyld.dylib   0x35cda873  
Thread 1 :
0   libsystem_kernel.dylib  0x35dab3c0  
1   libdispatch.dylib   0x35ca4f27  
Thread 2 (AXSpeech):
0   libsystem_kernel.dylib  0x35d96164  
1   CoreFoundation  0x24053905  
2   CoreFoundation  0x24051c8d  
3   CoreFoundation  0x23fa5249  
4   CoreFoundation  0x23fa5035  
5   Foundation  0x24d8f975  
6   libAXSpeechManager.dylib    0x34694791  
7   Foundation  0x24e60635  
8   libsystem_pthread.dylib 0x35e4c873  
9   libsystem_pthread.dylib 0x35e4c7e7  
10  libsystem_pthread.dylib 0x35e4a740  
Thread 3 (AVAudioSession Notify Thread):
0   libsystem_kernel.dylib  0x35d96164  
1   CoreFoundation  0x24053905  
2   CoreFoundation  0x24051c8d  
3   CoreFoundation  0x23fa5249  
4   CoreFoundation  0x23fa5035  
5   libAVFAudio.dylib   0x2298a16d  
6   libAVFAudio.dylib   0x2295ec87  
7   libsystem_pthread.dylib 0x35e4c873  
8   libsystem_pthread.dylib 0x35e4c7e7  
9   libsystem_pthread.dylib 0x35e4a740  
Thread 4 :
0   libsystem_kernel.dylib  0x35d96164  
1   CoreFoundation  0x24053905  
2   CoreFoundation  0x24051c8d  
3   CoreFoundation  0x23fa5249  
4   CoreFoundation  0x23fa5035  
5   Foundation  0x24d8f975  
6   Foundation  0x24dde58d  
7   libdispatch.dylib   0x35c90d2b  <redacted> + 10
8   libdispatch.dylib   0x35c9b535  
9   libdispatch.dylib   0x35c9faf1  
10  libdispatch.dylib   0x35c9f4d5  <redacted> + 96
11  libsystem_pthread.dylib 0x35e4ab45  _pthread_wqthread + 1024
12  libsystem_pthread.dylib 0x35e4a734  start_wqthread + 8
Thread 5 :
0   libsystem_kernel.dylib  0x35da9a60  
1   libsystem_pthread.dylib 0x35e4ccb1  
2   Foundation  0x24ddac27  
3   meipmusic   0x41b6bb    
4   Foundation  0x24e60635  
5   libsystem_pthread.dylib 0x35e4c873  
6   libsystem_pthread.dylib 0x35e4c7e7  
7   libsystem_pthread.dylib 0x35e4a740  
Thread 6 (com.apple.NSURLConnectionLoader):
0   libsystem_kernel.dylib  0x35d96164  
1   CoreFoundation  0x24053905  
2   CoreFoundation  0x24051c8d  
3   CoreFoundation  0x23fa5249  
4   CoreFoundation  0x23fa5035  
5   CFNetwork   0x23917ee7  
6   Foundation  0x24e60635  
7   libsystem_pthread.dylib 0x35e4c873  
8   libsystem_pthread.dylib 0x35e4c7e7  
9   libsystem_pthread.dylib 0x35e4a740  
Thread 7 (PF_AFNetworking):
0   libsystem_kernel.dylib  0x35d96164  
1   CoreFoundation  0x24053905  
2   CoreFoundation  0x24051c8d  
3   CoreFoundation  0x23fa5249  
4   CoreFoundation  0x23fa5035  
5   Foundation  0x24d8f975  
6   Foundation  0x24dde58d  
7   meipmusic   0x4495c3    
8   Foundation  0x24e60635  
9   libsystem_pthread.dylib 0x35e4c873  
10  libsystem_pthread.dylib 0x35e4c7e7  
11  libsystem_pthread.dylib 0x35e4a740  
Thread 8 (com.apple.CFSocket.private):
0   libsystem_kernel.dylib  0x35da9fb4  
1   libsystem_pthread.dylib 0x35e4c873  
2   libsystem_pthread.dylib 0x35e4c7e7  
3   libsystem_pthread.dylib 0x35e4a740  
Thread 9 (WebThread):
0   libsystem_kernel.dylib  0x35d96164  
1   CoreFoundation  0x24053905  
2   CoreFoundation  0x24051c8d  
3   CoreFoundation  0x23fa5249  
4   CoreFoundation  0x23fa5035  
5   WebCore 0x32e7f287  
6   libsystem_pthread.dylib 0x35e4c873  
7   libsystem_pthread.dylib 0x35e4c7e7  
8   libsystem_pthread.dylib 0x35e4a740  
Thread 10 (JavaScriptCore::Marking):
0   libsystem_kernel.dylib  0x35da9a60  
1   libsystem_pthread.dylib 0x35e4ccb1  
2   libc++.1.dylib  0x34c0786d  
3   JavaScriptCore  0x2565c0f5  
4   JavaScriptCore  0x2565c15f  
5   JavaScriptCore  0x2546dd51  
6   JavaScriptCore  0x2546dcaf  
7   libsystem_pthread.dylib 0x35e4c873  
8   libsystem_pthread.dylib 0x35e4c7e7  
9   libsystem_pthread.dylib 0x35e4a740  
Thread 11 (WebCore: CFNetwork Loader):
0   libsystem_kernel.dylib  0x35d96164  
1   CoreFoundation  0x24053905  
2   CoreFoundation  0x24051c8d  
3   CoreFoundation  0x23fa5249  
4   CoreFoundation  0x23fa5035  
5   WebCore 0x32ea308d  
6   JavaScriptCore  0x2546dd51  
7   JavaScriptCore  0x2546dcaf  
8   libsystem_pthread.dylib 0x35e4c873  
9   libsystem_pthread.dylib 0x35e4c7e7  
10  libsystem_pthread.dylib 0x35e4a740  
Thread 12 (AXSpeech):
0   libsystem_kernel.dylib  0x35d96164  
1   CoreFoundation  0x24053905  
2   CoreFoundation  0x24051c8d  
3   CoreFoundation  0x23fa5249  
4   CoreFoundation  0x23fa5035  
5   Foundation  0x24d8f975  
6   libAXSpeechManager.dylib    0x34694791  
7   Foundation  0x24e60635  
8   libsystem_pthread.dylib 0x35e4c873  
9   libsystem_pthread.dylib 0x35e4c7e7  
10  libsystem_pthread.dylib 0x35e4a740  
Thread 13 :
0   libsystem_kernel.dylib  0x35daa0c4  
1   libc++.1.dylib  0x34c468b9  
2   JavaScriptCore  0x25819ee3  
3   JavaScriptCore  0x25819cab  
4   JavaScriptCore  0x2581b807  
5   JavaScriptCore  0x2581b7a1  
6   libsystem_pthread.dylib 0x35e4c873  
7   libsystem_pthread.dylib 0x35e4c7e7  
8   libsystem_pthread.dylib 0x35e4a740  
Thread 14 (com.apple.coremedia.player.async):
0   libsystem_kernel.dylib  0x35d961b4  
1   MediaToolbox    0x25c3479d  
2   CoreMedia   0x2485ab41  
3   libsystem_pthread.dylib 0x35e4c873  
4   libsystem_pthread.dylib 0x35e4c7e7  
5   libsystem_pthread.dylib 0x35e4a740  
Thread 15 (WebCore: LocalStorage):
0   libsystem_kernel.dylib  0x35da9a60  
1   libsystem_pthread.dylib 0x35e4ccb1  
2   JavaScriptCore  0x25471577  
3   WebKitLegacy    0x33ad8f7d  
4   WebKitLegacy    0x33ad889d  
5   JavaScriptCore  0x2546dd51  
6   JavaScriptCore  0x2546dcaf  
7   libsystem_pthread.dylib 0x35e4c873  
8   libsystem_pthread.dylib 0x35e4c7e7  
9   libsystem_pthread.dylib 0x35e4a740  
Thread 16 :
0   libsystem_kernel.dylib  0x35d96164  
1   CoreFoundation  0x24053905  
2   CoreFoundation  0x24051c8d  
3   CoreFoundation  0x23fa5249  
4   CoreFoundation  0x23fa5035  
5   TTSSpeechBundle 0x3512009   
6   libsystem_pthread.dylib 0x35e4c873  
7   libsystem_pthread.dylib 0x35e4c7e7  
8   libsystem_pthread.dylib 0x35e4a740  
Thread 17 (com.apple.coreaudio.AQClient):
0   libsystem_kernel.dylib  0x35d96164  
1   CoreFoundation  0x24053905  
2   CoreFoundation  0x24051c8d  
3   CoreFoundation  0x23fa5249  
4   CoreFoundation  0x23fa5035  
5   AudioToolbox    0x2342c5ad  
6   AudioToolbox    0x2341551f  
7   libsystem_pthread.dylib 0x35e4c873  
8   libsystem_pthread.dylib 0x35e4c7e7  
9   libsystem_pthread.dylib 0x35e4a740  
Thread 18 :
0   libsystem_kernel.dylib  0x35daa92c  
1   libsystem_pthread.dylib 0x35e4a734  start_wqthread + 8
Thread 19 (com.apple.coremedia.player.async):
0   libsystem_kernel.dylib  0x35d961b4  
1   MediaToolbox    0x25c3479d  
2   CoreMedia   0x2485ab41  
3   libsystem_pthread.dylib 0x35e4c873  
4   libsystem_pthread.dylib 0x35e4c7e7  
5   libsystem_pthread.dylib 0x35e4a740  
Thread 21 :
0   libsystem_kernel.dylib  0x35daa92c  
1   libsystem_pthread.dylib 0x35e4a734  start_wqthread + 8
Thread 22 :
0   libsystem_kernel.dylib  0x35daa92c  
1   libsystem_pthread.dylib 0x35e4a734  start_wqthread + 8

At a minimum, this is UI code that is definitely running on a background queue: 至少,这是绝对在后台队列上运行的UI代码:

    if self.chartsSwitch.on {

I assume this line is as well: 我认为这行也是:

        chart["title"] = self.songTitleText.text

You cannot access UIKit on a background queue. 您不能在后台队列上访问UIKit。 You must collect all this data on the main queue, and pass it to things that run in the background. 您必须在主队列上收集所有这些数据,并将其传递给在后台运行的事物。

I assume this entire block is actually on a background queue (since you load an NSData from an URL which should not be done on the main queue). 我假设整个块实际上都在后台队列中(因为您是从URL加载NSData ,因此不应在主队列中完成此操作)。 In that case, this is a mutating UI change on a background queue: 在这种情况下,这是后台队列上的变异UI更改:

indicator.startAnimation()

I wouldn't be surprised if there is more of this. 如果还有更多,我不会感到惊讶。 You have to separate your model code and networking code from your UI code. 您必须将模型代码和网络代码与UI代码分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “仅在主线程上运行”异常仅限于模拟器 - “Only run on the main thread” exception is simulator only Xcode 9.0 Swift 04(由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“仅在主线程上运行!”) - Xcode 9.0 Swift 04 (Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!') 由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“仅在主线程上运行!” - Terminating app due to an uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!' &#39;NSInternalInconsistencyException&#39;,原因:&#39;只在主线程上运行!&#39; 错误 - 'NSInternalInconsistencyException', reason: 'Only run on the main thread!' error iOS Swift仅在应用程序的主线程错误上运行 - iOS Swift Only run on the main thread error for app 在主线程上运行闭包 - Run closure on main thread 为什么要在主线程上运行? - Why does this run on main thread? 宏在主线程上运行方法 - Macro to run method on main thread 异常“必须在主线程上进行调用”iOS - Exception 'Call must be made on main thread' iOS uikit 方法是否保证在主线程上运行? - Are uikit methods guaranteed to be run on main thread?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM