I've been trying to show the progressHUD when my cell loads, and it should show how the download progress are doing. so i placed the code in SDWebimage progress block to get the download progress and pass it into MBProgress HUD to work but i don't know what i am doing wrong here !
let url = NSURL(string: (array[indexPath.row][0] as? String)!)
cell.imageView.sd_setImageWithURL(url, placeholderImage: nil, options: nil, progress: { (value1, value2) -> Void in
self.HUD = MBProgressHUD(view: self.mycollectionView)
self.view.addSubview(self.HUD)
self.HUD.mode = MBProgressHUDMode.AnnularDeterminate
self.HUD.delegate = self
self.HUD.show(true)
var x : Float = Float(value1)
self.HUD.progress = x
println(value1)
println(value2)
}, completed: block)
I am getting also an error saying : 'MBProgressHUD needs to be accessed on the main thread.'
You can only update UI from the main thread, just as your error says. Downloading the image is an asynchronous operation that is not performed on the main thread, the callback block therefore can't update the UI - unless you perform the UI updates on the main thread. To do that, you have to use gcd to perform your code on the main thread, try this:
let url = NSURL(string: (array[indexPath.row][0] as? String)!)
cell.imageView.sd_setImageWithURL(url, placeholderImage: nil, options: nil, progress: { (value1, value2) -> Void in
dispatch_async(dispatch_get_main_queue(), {
self.HUD = MBProgressHUD(view: self.mycollectionView)
self.view.addSubview(self.HUD)
self.HUD.mode = MBProgressHUDMode.AnnularDeterminate
self.HUD.delegate = self
self.HUD.show(true)
var x : Float = Float(value1)
self.HUD.progress = x
})
println(value1)
println(value2)
}, completed: block)