How does async/await work?

Hi folks,

I am downloading some images from Firebase and adding them to main bundle resources, after which I use them. I am having trouble with making my code to wait for the downloads to get complete before it executes the next statement.

I tried completion handlers, async/await but still the same.

Not sure if I am doing it the right way.

Can anyone help what is the correct approach for this scenario.

override func viewWillAppear(_ animated: Bool) {

        super.viewWillAppear(animated)

        // Create a session configuration

         let configuration = ARImageTrackingConfiguration()

            Task{

                await self.checkForDownloads.refreshResources(forUser: self.username)

            }

            if let trackedImages = ARReferenceImage.referenceImages(inGroupNamed: "AR Resources", bundle: Bundle.main){

                configuration.trackingImages = trackedImages

                configuration.maximumNumberOfTrackedImages = 1

                print("Images Found: \(trackedImages.count)")

            }

        sceneView.session.run(configuration)

    }

   
func refreshResources(forUser username: String) async {
//..
}

I am expecting checkForDownloads.refreshResources to finish downloading first, then proceed to next statement.

Replies

All the code you want to execute after refreshResources should be inside the Task block.

  • Just tried that, still getting the same result

  • Not sure what's in refreshResources, probably returning prior to completing the downloads. Are you using async versions of network calls?

  • downloading a few images from FireStore using its getData(maxSize:) method, and it is actually returning prior to completing the download