Swift 3 warning for dispatch async
Asked Answered
S

4

34

I have this code:

DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.default).async {
                let url = URL(string: itemImageURL )
                let data = try? Data(contentsOf: url!)
                if data != nil {
                    DispatchQueue.main.async{
                        cell.advImage!.image = UIImage(data: data!)
                    }
                }
            }

I get this warning in Swift 3:

'default' was deprecated in iOS 8.0: Use qos attributes instead

on the first line.

Haven't found yet a solution. Has anybody?

Sicilia answered 30/9, 2016 at 11:59 Comment(2)
see this #39639251Darwin
tried it and did not work. the checked answer is what worked fine for me.Sicilia
W
68

try qos: DispatchQoS.QoSClass.default instead of priority: DispatchQueue.GlobalQueuePriority.default

DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
            let url = URL(string: itemImageURL )
            let data = try? Data(contentsOf: url!)
            if data != nil {
                DispatchQueue.main.async{
                    cell.advImage!.image = UIImage(data: data!)
                }
            }
        }
Wasp answered 30/9, 2016 at 12:9 Comment(1)
Since .default is the default, you can also just use: DispatchQueue.global().async { ... }Fondle
S
5

Instead of using priority parameter:

DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.default).async {
 // ...
}

use qos parameter that uses a different enum DispatchQoS.QoSClass.default but you can also use its enum value as just .default:

DispatchQueue.global(qos: .default).async {
 // ...
}

Swift 3 has brought many changes on GCD(Grand Central Dispatch).

Shanonshanta answered 30/9, 2016 at 12:14 Comment(0)
C
1

If you're creating a property using the Dispatch Framework and updating the UI with some animation within a function it might look something like this.

let queue = DispatchQueue.global(qos: DispatchQoS.QoSClass.default)
    // dispatch_after says that it will send this animation every nsec
    queue.asyncAfter(deadline: when) {
        DispatchQueue.main.async(execute: {
            self.animate(withDuration: 0.5, animations: {
                self.image.setWidth(35)
                self.image.setHeight(35)
            })
        })
    }
Crystie answered 4/11, 2016 at 14:29 Comment(0)
N
1

Below code is tested for Swift 3.0 on Xcode 8.2.1

DispatchQueue.global(qos: .background).async {
            let img2 = Downloader.downloadImageWithURL(imageURLs[1])

            // Background Thread
            DispatchQueue.main.async {

                // Run UI Updates
                self.imageView2.image = img2
            }
        }

where property of QoS are :

background, utility, `default`, userInitiated, userInteractive and unspecified

Refer this apple document for more details.

Nava answered 17/3, 2017 at 5:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.