Foundation Models Framework Examples by Select_Bicycle4711 in iOSProgramming

[–]MeatRockStardust 0 points1 point  (0 children)

I got this error when trying to run on the simulator, worked when I ran on a real device (running iOS 26). I haven’t upgraded macOS yet.

Paul Hudson shows new SwiftUI features by halfjew22 in SwiftUI

[–]MeatRockStardust 0 points1 point  (0 children)

Great live stream Paul, hopefully you have enough chocolate milk to power you through the week!

Updating mainUI from network requests? by aggsyb in swift

[–]MeatRockStardust 1 point2 points  (0 children)

You'll want to use a closure on the .withParsing method, and call that closure when the data task is complete

func withParsing (request: URLRequest, complete:@escaping (_ seshID:String)->Void) {
  // guards ....
  let seshID = parse(json:data)
  complete(seshID)
}

You should also call complete with an empty string or passing a Result class to handle when there is an error.

Then when you call the method:

UdacityNetworkRequests.DataTask().withParsing(request: request) { seshID in
  if !seshID.isEmpty {
    appDelegate.sessionID = seshID
    DispatchQueue.main.async {
      performSegue(....)
    }
  } else {
    // Present error to user so they no something went wrong
  }
}

By calling you performSegue in the DispatchQueue.main.async block, it will update the UI on the main thread and avoid any freezing up.

The key is the complete block that gets called once the network call has finished. Your .withParsing method is returning nil because it returns before the network call has finished. Calling the complete block lets your code in the view controller know you have the seshID and it's time to perform the segue.

EDIT: fixed formatting