仅在主线程异常上运行

时间:2015-10-19 18:57:11

标签: ios multithreading swift

我正在跟踪我的应用中的崩溃情况。这个特殊的崩溃给了我错误"由于未被捕获的异常终止应用程序' NSInternalInconsistencyException',原因:'仅在主线程上运行!'"。奇怪的是,我似乎无法使用我的设备或模拟器重新创建崩溃,但在我的分析中,它表明这种崩溃发生了很多。我被告知这种情况正在发生,因为你只能在主线程上进行UI更新,所以我更新了我的代码,但这种崩溃一直在发生,我仍然无法在手机或模拟器上重新创建崩溃。

这是崩溃发生的代码:

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.")
        });

    }
})

崩溃:

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

1 个答案:

答案 0 :(得分:0)

至少,这是肯定在后台队列上运行的UI代码:

    if self.chartsSwitch.on {

我认为这一行也是如此:

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

您无法在后台队列中访问UIKit。您必须在主队列上收集所有这些数据,并将其传递给在后台运行的内容。

我假设整个块实际上是在后台队列上(因为你从NSData加载了URL,这不应该在主队列上完成。在这种情况下,这是后台队列上的UI变化:

indicator.startAnimation()
如果有更多的话,我不会感到惊讶。您必须将模型代码和网络代码与UI代码分开。