1

So the app works in test mode but as soon as I went to build for release I got this main thread issue.

UIImageView.image must be used from main thread only

According the the error I am not calling something on the main thread, yet the line it has thrown the thread error at is blank (see screenshot)

So I can only guess what they talking about is the code directly under that line?

thread issue

code

@objc func nowplaying(){
          let jsonURLString = "https://api.drn1.com.au/station/playing"
                  guard let feedurl = URL(string: jsonURLString) else { return }

                  URLSession.shared.dataTask(with: feedurl) { (data,response,err)
                      in

                      guard let data = data else { return }

                      do{
                          let nowplaying = try JSONDecoder().decode(Nowplayng.self, from: data)

                          nowplaying.data.forEach {

                              DispatchQueue.main.async {


                              self.artist.text = nowplaying.data.first?.track.artist
                              self.song.text = nowplaying.data.first?.track.title

                              }


                              print($0.track.title)

                              if var strUrl = nowplaying.data.first?.track.imageurl {

                                  strUrl = strUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
                                  self.imageurl.kf.setImage(with: URL(string: strUrl), placeholder: nil)

                                //MusicPlayer.shared.nowplaying(artist:  $0.track.artist, song: $0.track.title, cover:strUrl)
                                MusicPlayer.shared.getArtBoard(artist: $0.track.artist, song: $0.track.title, cover:strUrl)
                              }


                         }

I can only guess it is because kingfisher wants a loading picture or something. But unclear?

RussellHarrower
  • 6,470
  • 21
  • 102
  • 204

2 Answers2

1

It is your responsibility to call Kingfisher's UI-extension methods on UI thread.

Before:

self.imageurl.kf.setImage(with: URL(string: strUrl), placeholder: nil)

After:

DispatchQueue.main.async {
    self.imageurl.kf.setImage(with: URL(string: strUrl), placeholder: nil)
}
Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93
0

I believe you could always run your code in main thread in your own processor, by using: king fisher process image on download thread so that can cause this problem

public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image?
    return DispatchQueue.main.sync {
        let image = ... // Your code needs to be performed in UI thread
        return image
    }
}
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49