Xcode Warning for iOS 16 : Please switch to an asynchronous networking API such as URLSession

"Synchronous URL loading of https://xyz.png 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."

Xcode shows above warning message when running my code on device with iOS version 16. While showing Virtualobject on the screen,

  func getMatrials(scene:[SCNNode]){

        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5 ) {  for child in scene {

          material.diffuse.contents = "https//xyz.png"

          //setting here materials with the png images from url.
         }
    }}

Above code works fine till the iOS v15.6. But showing warning in iOS 16 and leading to delay in showing model on the screen due to main.asynafter(deadline).

I tried to fix the issue for iOS 16 with below code:

let url1 = URL(string: "https//xyz.png") DispatchQueue.global().async {

                                                let data1 = try? Data(contentsOf: url1!)

                                                DispatchQueue.main.async {

                                                 material.diffuse.contents = UIImage(data: data1!)

          }

But the model loading delay with more time is still there. Any one could suggest a solution to me or where I am wrong.

Answered by negiruh in 730762022

ARKIT

Below code works fine with iPhone with iOS version 15.6.

Also instead of assigning url to the material contents, if I use default image from resources its loading on time.

Tag: ProcessRaycastResults
private func setVirtualObject3DPosition(_ results: [ARRaycastResult], with virtualObject: VirtualObject) {

        guard let result = results.first else {

            print("Unexpected case: the update handler is always supposed to return at least one result.")

            return;

        }
        self.setTransform(of: virtualObject, with: result)

        // If the virtual object is not yet in the scene, add it.

        if virtualObject.parent == nil {
           self.childNodesPortal = virtualObject.childNodes

           self.getMatrials(scene: virtualObject.childNodes) 

        }
     }

func getMatrials(scene:[SCNNode]){
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5 ) { 
          for child in scene {

             material.diffuse.contents = "https//xyz.png"
             material.normal.contents = "https//xss.png"
            //setting here materials with the png images from url.
         }
    }
}

Ios version 16 causing delay for image assignment for the virtual object.

Anyone could suggest why there is variation for iOS v16 and what workaround I should do.

Thanks in advance.

You can use : `let (data, response) = try await URLSession.shared.data(from: url)

Accepted Answer

ARKIT

Below code works fine with iPhone with iOS version 15.6.

Also instead of assigning url to the material contents, if I use default image from resources its loading on time.

Tag: ProcessRaycastResults
private func setVirtualObject3DPosition(_ results: [ARRaycastResult], with virtualObject: VirtualObject) {

        guard let result = results.first else {

            print("Unexpected case: the update handler is always supposed to return at least one result.")

            return;

        }
        self.setTransform(of: virtualObject, with: result)

        // If the virtual object is not yet in the scene, add it.

        if virtualObject.parent == nil {
           self.childNodesPortal = virtualObject.childNodes

           self.getMatrials(scene: virtualObject.childNodes) 

        }
     }

func getMatrials(scene:[SCNNode]){
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5 ) { 
          for child in scene {

             material.diffuse.contents = "https//xyz.png"
             material.normal.contents = "https//xss.png"
            //setting here materials with the png images from url.
         }
    }
}

Ios version 16 causing delay for image assignment for the virtual object.

Anyone could suggest why there is variation for iOS v16 and what workaround I should do.

Thanks in advance.

Xcode Warning for iOS 16 : Please switch to an asynchronous networking API such as URLSession
 
 
Q