learningthings
learningthings

Reputation: 73

Terminating app due to an uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!'

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.

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!
        self.postButton.hidden = true
        self.addPictureButton.hidden = true
        self.indicator.stopAnimation(false, completion: nil)
        self.navigationController!.pushViewController(self.storyboard!.instantiateViewControllerWithIdentifier("ShareViewController") , animated: true)

    } else {
        self.indicator.stopAnimation(false, completion: nil)
        self.helper.showErrorAlert("Couldn't save your song please try again.")
    }
})

Upvotes: 1

Views: 3541

Answers (1)

t4nhpt
t4nhpt

Reputation: 5302

The error message is Only run on the main thread!. The reason is you update UI in background thread (testObject.saveInBackgroundWithBlock ({ (success, error) -> Void in). Check your updating UI task, such as hide button, stop animation, navigate..., and move it to main thread.

dispatch_async(dispatch_get_main_queue(), ^{
/* Your UI code */
});

Upvotes: 2

Related Questions