我有一个显示从 API 获取数据的列表的基本视图。我想在从 API 检索数据时实现一个活动指示器。在 MVC 中,我们可以使用委托和协议,让视图控制器继承协议,在模型完成获取数据后,我们调用委托告诉视图控制器数据已完成检索(现在隐藏活动指示器等.) 如何在 SwiftUI 中实现类似的东西,它是 MVVM 风格?
我已经尝试从这个问题实现一个活动指示器,我只是不知道如何以及何时停止它:SwiftUI 中的活动指示器
我的 SourcesViewModel(它从 newsapi.org 获取新闻文章来源)
import UIKit
class SourcesViewModel: Identifiable {
let id = UUID()
let source: Sources
init(source: Sources) {
self.source = source
}
var name: String {
return self.source.sourceName
}
var description: String {
return self.source.sourceDescription
}
}
Run Code Online (Sandbox Code Playgroud)
我的 SourcesListViewModel:
import Combine
class SourcesListViewModel: ObservableObject {
init() {
fetchSources()
}
@Published var sources = [SourcesViewModel]()
private func fetchSources() {
NetworkManager.shared.getSourceData { (sources) in
self.sources …Run Code Online (Sandbox Code Playgroud) 当使用新的 WKDownloadDelegate 时,我尝试下载一个文件,第一次下载正常,但是当我尝试再次下载同一文件时,WKDownloadDelegate 调用此方法:
\nfunc download(_ download: WKDownload, didFailWithError error: Error, resumeData: Data?)\nRun Code Online (Sandbox Code Playgroud)\n当我将其打印到控制台时:
\ndownload(_ download: <WKDownload: 0x7fc58fb2dfd0>, didFailWithError error: The operation couldn\xe2\x80\x99t be completed. (NSURLErrorDomain error -3000.), resumeData: nil\nRun Code Online (Sandbox Code Playgroud)\n问题是我找不到有关此错误(-3000)的任何信息,因此任何建议都会很好。
\n这是整个 WKDownloadDelegate 代码
\n public func download(_ download: WKDownload, decideDestinationUsing response: URLResponse, suggestedFilename: String, completionHandler: @escaping (URL?) -> Void) {\n let tempDirectory = FileManager.default.temporaryDirectory\n let tempFolderName = UUID().uuidString\n let tempDirectoryPath = tempDirectory.appendingPathComponent(tempFolderName)\n try? FileManager.default.createDirectory(at: tempDirectoryPath, withIntermediateDirectories: false)\n let url = tempDirectory.appendingPathComponent(suggestedFilename)\n currentDownloadedPreviewItemUrl = url\n …Run Code Online (Sandbox Code Playgroud)