Xcode 模拟器如何在互联网连接关闭时从互联网获取数据?

How Xcode simulator is fetching data from internet when the internet connection is OFF?

我创建了一个简单的 SwiftUI 应用程序,它应该显示从 iTunes 中获取的歌曲列表 url link:

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)
        }
    }
.onAppear(perform: loadData)
}

   func loadData() {
guard let url = URL(string: "https://itunes.apple.com/search?term=taylor+swift&entity=song") else {
    print("Invalid URL")
    return
}
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { data, response, error in
  if let data = data {
        if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
            DispatchQueue.main.async {
                self.results = decodedResponse.results
            }
        
            return
        }
    }
    print("Fetch failed: \(error?.localizedDescription ?? "Unknown error")")
}.resume()
}
}

当我的 Mac 上的互联网连接打开时,我能够获取数据并将其加载到列表中。我决定将其关闭,然后我完全 closed/deleted 内存中的应用程序,然后再次打开它的应用程序,数据仍然存在。这让我很困惑,谁能解释一下发生了什么,因为代码应该只在 Wi-Fi 开启时加载数据,为什么我在关闭时仍然能看到它?

Short video of my simulator

My question is, why here is gone?

如果不更改配置,

URLSession.shared默认缓存数据。只需将请求编辑为

let request = URLRequest(url: url, cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalCacheData)

只需尝试使用 .reloadIgnoringLocalCacheData。它对我有用。