[英]iOS Swift Multithreading Blocking Situation
我正在嘗試構建一個應用程序,該應用程序使用大中央調度在后台下載某些圖像。 但是,即使我使用全局隊列來獲取不同的線程並下載圖像,它也會阻塞主線程。 我的代碼由兩個 swift 類組成,一個是 DownloadRequestViewController,另一個是 DownloadHandler。 他們來了:
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
})
}
這是我在 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)
})
}
我究竟做錯了什么? 任何幫助將不勝感激,謝謝...我在下載章節方法中做什么重要嗎?
請更改此行
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
到這一行
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {
這將在具有后台優先級的線程上執行調度閉包內的代碼,這不會“凍結”主/UI 線程
更新
//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")!)
我認為這有效:
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.