URLSession.shared.data(from: url) fails in Swift Playgrounds for Mac

I have an app using UrlSession in Swift Playgrounds for iPad, and everything worked great. I opened the same code in Swift Playgrounds for Mac, and the line:

let (data, _) = try await URLSession.shared.data(from: url)

silently fails everytime. I exported a small snippet to a playground in Mac, and found that adding:

import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true

At the beginning of the script fixes this in the playground. I am wondering if there is a way to move this code to the app version and get this up and running on the Mac? Or maybe this code was never designed to run on a Macbook?

Answered by jakee417 in 718350022

I started from: https://www.hackingwithswift.com/forums/swiftui/urlsession-shared-data-from-url-fails-in-swift-playgrounds-for-mac/14987

in the playground, it is clear to me where needsIndefiniteExecution = true

goes, simply at the top of the script. In the app I am making, I don’t know where it goes. When I put it at the top of the file, it complains about not being able to run in a global scope. Is there a difference between the playground and the app (made in Swift playgrounds)?

adding … needsIndefiniteExecution = true … fixes this in the playground.

That’s pretty standard practice. Which you created your playground, what template did you start from?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

I started from: https://www.hackingwithswift.com/forums/swiftui/urlsession-shared-data-from-url-fails-in-swift-playgrounds-for-mac/14987

in the playground, it is clear to me where needsIndefiniteExecution = true

goes, simply at the top of the script. In the app I am making, I don’t know where it goes. When I put it at the top of the file, it complains about not being able to run in a global scope. Is there a difference between the playground and the app (made in Swift playgrounds)?

In the app I am making

So this is the key point. If you’re creating a playground, you need to set needsIndefiniteExecution and it makes sense to do that at the top of your playground. OTOH, if you’re creating an app, that’s not necessary because apps run indefinitely already.

So, that’s the reason for early my question. When you starting writing code, did you choose File > New App? Or File > New Blank Playground?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I misread your previous post. Here is a reproducible example, start with File > New App and then a simple app using a network call:

import SwiftUI

struct Response: Codable {
  var results: [Result]
}

struct Result: Codable {
  var trackId: Int
  var trackName: String
  var collectionName: String
}

struct ContentView: View {
  @State private var results = [Result]()
   
  var body: some View {
    List(results, id: \.trackId) { item in
      VStack(alignment: .leading) {
        Text(item.trackName)
          .font(.headline)
        Text(item.collectionName)
      }
    }
    .task {
      results = await loadData()
    }
  }
}

func loadData() async -> [Result]{
  var results = [Result(trackId: -1, trackName: "No Tracks Available", collectionName: "No Collection Available")]
   
  let url = URL(string: "https://itunes.apple.com/search?term=taylor+swift&entity=song")
   
  do {
    let (data, _) = try await URLSession.shared.data(from: url!)
     
    if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
      results = decodedResponse.results
    }
  } catch {
    print("Invalid data")
  }
  return results
}

For which I saw: Seems like I might have a wrong setting or something? I have tried this on two separate macbooks with no success on either.

Nudging the thread… is this a bug or am I missing something?

URLSession.shared.data(from: url) fails in Swift Playgrounds for Mac
 
 
Q