简体   繁体   English

iOS Swift 多线程阻塞情况

[英]iOS Swift Multithreading Blocking Situation

I am trying to build an app that downloads some certain images in the background with using grand central dispatch.我正在尝试构建一个应用程序,该应用程序使用大中央调度在后台下载某些图像。 However even if I use the global queue to get a different thread and download the image it blocks the main thread.但是,即使我使用全局队列来获取不同的线程并下载图像,它也会阻塞主线程。 My code consists of two swift classes one is DownloadRequestViewController and other is DownloadHandler.我的代码由两个 swift 类组成,一个是 DownloadRequestViewController,另一个是 DownloadHandler。 Here they are:他们来了:

class DownloadHandler: NSObject {

    let userDefaults = NSUserDefaults.standardUserDefaults()
    var filePath : String = "Turk Isi Manga"

    override init() {
        super.init()
        NSNotificationCenter.defaultCenter().addObserver(self , selector: "downloadChapter:", name: "downloadListNotification", object: DisplayMangaViewController.self)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "downloadChapter:", name: "downloadListNotification", object: DownloadRequestListViewController.self)
    }

    func downloadChapter(notification : NSNotification){
        dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
             //Downloading image and save it to memory here
         })
    }

And here is how I call this class and the downloadChapter in DownloadRequestViewController swift class:这是我在 DownloadRequestViewController swift 类中调用这个类和 downloadChapter 的方式:

@IBAction func downloadBarButton(sender: AnyObject) {
        dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
            var obtainedMangaNameArray = [self.obtainedMangaName]
            let userInfo = ["downloadList" : self.selectedChapters , "mangaName" : obtainedMangaNameArray]
            var sdfs = DownloadHandler()
            let notification = NSNotification(name: "downloadListNotification", object: DownloadRequestListViewController.self, userInfo: userInfo)
            sdfs.downloadChapter(notification)
            //NSNotificationCenter.defaultCenter().postNotification(notification)
        })
    }

What am I doing wrong?我究竟做错了什么? Any help would be appreciated, thanks... Does it matter what I do in download chapter method?任何帮助将不胜感激,谢谢...我在下载章节方法中做什么重要吗?

Please change this line请更改此行

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {

to this line到这一行

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {

This will execute the code inside the dispatch closure on a thread with background priority, which will not "freeze" the main/UI thread这将在具有后台优先级的线程上执行调度闭包内的代码,这不会“冻结”主/UI 线程

UPDATE更新

//Downloader Class

    class DownloadHandler: NSObject {
        class func downloadImageFromURL (url: NSURL) {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { () -> Void in
                if let imageData: NSData = NSData(contentsOfURL: url) {
                    imageData.writeToFile("Your local Path", atomically: false)
                }
            })
        }
    }

    //Usage example
    DownloadHandler.downloadImageFromURL(NSURL(string: "Your URL To Image")!)

I think this works:我认为这有效:

func downloadChapter(notification : NSNotification){
        //fetch on different thread
        let qos = Int(QOS_CLASS_USER_INITIATED.value)
        dispatch_async(dispatch_get_global_queue(qos, 0), {
            //download here

            //back to main thread
            dispatch_async(dispatch_get_main_queue(), {
                //if there is something more you wanna do or you can just go back
            })
        })
}

@IBAction func downloadBarButton(sender: AnyObject) {
        var obtainedMangaNameArray = [self.obtainedMangaName]
        let userInfo = ["downloadList" : self.selectedChapters , "mangaName" : obtainedMangaNameArray]
        var sdfs = DownloadHandler()
        let notification = NSNotification(name: "downloadListNotification", object: DownloadRequestListViewController.self, userInfo: userInfo)
        sdfs.downloadChapter(notification)
        //NSNotificationCenter.defaultCenter().postNotification(notification)
}

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

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