【问题标题】:Terminating app due to an uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!'由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“仅在主线程上运行!”
【发布时间】:2015-10-16 01:33:38
【问题描述】:

我正在跟踪我的应用程序中的崩溃。这个特定的崩溃给了我错误“由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'仅在主线程上运行!'”。奇怪的是,我似乎无法使用我的设备或模拟器重新创建崩溃,但在我的分析中它表明这种崩溃发生了很多。

这是发生崩溃的代码:

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

【问题讨论】:

    标签: ios multithreading swift


    【解决方案1】:

    错误消息是Only run on the main thread!。原因是您在后台线程 (testObject.saveInBackgroundWithBlock ({ (success, error) -> Void in) 中更新 UI。检查您的 UI 更新任务,例如隐藏按钮、停止动画、导航...,并将其移至主线程。

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

    【讨论】:

    • 好吧,这就是我的想法,但我想知道为什么我似乎无法触发崩溃,但在我的崩溃分析中,这种情况经常发生。我正在做我的用户正在做的完全相同的事情,但应用程序并没有为我崩溃。
    • 我猜它应该延迟更新你的 UI,因为你在后台线程中执行更新
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 2012-07-28
    • 2015-12-19
    • 1970-01-01
    相关资源
    最近更新 更多