How To update dispatch_async to Swift 3

Hello! I am updating my app (I'm a little far behind lol) to Swift 3. I have a line of code in my app that uses dispatch_async and dispatch_get_main_queue() But, now it's been updated in Swift 3, so can you help me update this code to swift 3? Here's the snippet of the code: (line 04 is where I am having the issue)


             //        Create an dataTask and pass in the quest
            let dataTask = session.dataTask(with: request, completionHandler: { (data, response, error) in
            
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
            
//                Get a reference to the imageView element of the cell
                let imageView = cell.viewWithTag(1) as! UIImageView
            
//                Get an image object from the data and assign it into the imageView
                imageView.image = UIImage(data: data!)
            
            })
        
        })
        
        dataTask.resume()
        
        }
    
        return cell


Thanks for the help!

Replies

Write :


DispatchQueue.main.async


To create your own queue :


DispatchQueue.global(qos: .userInitiated).async


But normally, this is translated automatically from Swift 2 to Swift 3

So, I'm sort of confused (sorry). What do I need to replace my line 04 with. I've been troubleshooting, and I can't figure it out. Sorry...

The following code is what you expected T_T:

DispatchQueue.main.async {

//your code

}

So, your code will be:


//        Create an dataTask and pass in the quest
            let dataTask = session.dataTask(with: request, completionHandler: { (data, response, error) in
           
                DispatchQueue.main.async {            
//                Get a reference to the imageView element of the cell
                let imageView = cell.viewWithTag(1) as! UIImageView       
//                Get an image object from the data and assign it into the imageView
                imageView.image = UIImage(data: data!      
            }
       
        })
       
        dataTask.resume()
       
        }
   
        return cell