简体   繁体   中英

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!'". 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.

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:

    if self.chartsSwitch.on {

I assume this line is as well:

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

You cannot access UIKit on a background queue. 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). In that case, this is a mutating UI change on a background queue:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM