-1

In Xcode 14 & iOS 16 - purple warning when downloading an image from URL with NSData dataWithContentsOfURL

Synchronous URL loading of <URL> should not occur on this application's main thread as it may lead to UI unresponsiveness. Please switch to an asynchronous networking API such as URLSession.

How to resolve this issue? Is anyone facing this same issue and resolving it?

PrasathBabu
  • 4,716
  • 4
  • 19
  • 35

2 Answers2

0

As the warning and documentation say, the method involves work that shouldn't be performed on the main thread. In your case it seems you're trying to download remote data using this method. It takes time, sometimes more than expected, and sometimes doesn't finish at all.

For downloading data from internet, you can use NSURLSession and its tasks (e.g., this one). In this case, the work is dispatched to one of background threads, and when downloading finishes, you're notified in the completionHandler.

If your UI expects retrieving data synchronously, you should reconsider. UI should show something else until data is downloaded, allowing the user to interact with the app. Only after the download is done, UI should be updated accordingly.

lazarevzubov
  • 1,767
  • 2
  • 14
  • 24
-1

Running the method into a separate thread resolved this issue.

NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(DownloadFile:) object:objects]; 
[thread start];
PrasathBabu
  • 4,716
  • 4
  • 19
  • 35