ios - 如何在 Swift 中使用 URLSession 添加 UIActivityIndi​​catorView?

标签 ios json swift uiactivityindicatorview urlsession

我有一个从 JSON 解析数据的 Master-Detail 项目。目的是在等待数据被获取并加载到 DetailsViewController 时,将 UIActivityIndi​​catorView(通过使用 URLSession)添加到 DetailsViewController。我尝试了几种方法,在以下之后在 Master 中启动 UIActivityIndi​​catorView:

    let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in

我也不知道在哪里停止它,我已经在 DetailViewController 的 ViewDidLoad() 中尝试过(在 configureView() 之前):

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    configureView()
}

但是也没用。我无法在任何地方找到有关使用 URLSession 的状态添加事件指示器的信息。在这里,我从 MasterViewController 添加了我试图启动事件指示器的代码:

let arrayOfUrls = [ URL(string: "http://www.omdbapi.com/?t=The+Dark+Knight&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Lord+of+the+Rings%3A+The+Return+of+the+King&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=Forrest+Gump&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=Inception&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Matrix&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=Interstellar&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Pianist&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Intouchables&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Departed&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Prestige&apikey=f85dc75e") ]


    for url in arrayOfUrls {

        let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in

        if let error = error {
            print (error)
        } else {

            if let data = data {

                do {                        
                    let movie = try JSONDecoder().decode(Movie.self, from: data)
                    print(movie.Title)
                    self.objects.append(movie.Title)
                    self.details.append(movie)
                } catch {                        
                    print("Json Processing Failed")                        
                }
            }
        }
        }
        task.resume()
    }
}

最佳答案

创建 NetworkService 类并在 func 中执行 api 调用,这种方式要好得多。

class NetworkService {

let arrayOfUrls = [ URL(string: "http://www.omdbapi.com/?t=The+Dark+Knight&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Lord+of+the+Rings%3A+The+Return+of+the+King&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=Forrest+Gump&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=Inception&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Matrix&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=Interstellar&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Pianist&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Intouchables&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Departed&apikey=f85dc75e"), URL(string: "http://www.omdbapi.com/?t=The+Prestige&apikey=f85dc75e") ]


func getData(completion:@escaping(Movie)->()){
    for url in arrayOfUrls {

    let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
    var movie = Movie()
    if let error = error {
        print (error)
    } else {

        if let data = data {

            do {                        
                movie = try JSONDecoder().decode(Movie.self, from: data)
                print(movie.Title)
                self.objects.append(movie.Title)
                self.details.append(movie)
            } catch {                        
                print("Json Processing Failed")                        
            }
        }
    }
     completion(movie)
    }
    task.resume()
}

}

在 View Controller 中调用你的函数:

  let networkService = NetworkService()
    activityIndicator.startAnimating()
    networkService.getData { result in
    self.activityIndicator.stopAnimating()
    //result your movie data do whatever yo want it
    DispatchQueue.main.async {
    //If you need to reload tableview or etc. do here
   }
 }

关于ios - 如何在 Swift 中使用 URLSession 添加 UIActivityIndi​​catorView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54031942/

相关文章:

ios - 从 vimeo 获取视频缩略图

json - 取消转义 JSON 响应字符串中的字符

json - 如何向每个 header 添加 json 网络 token ?

ios - 如何将现有对象添加到 Parse.com Databrowser 中的关系中

ios - 是否可以通过 wifi 上传 .ml 模型文件而不是嵌入它们?

iOS私有(private)API调用不显示手机APP的方法?

java - Android - Base 64 中的大量数据,如何处理它

swift - Swift 4 中的键值观察

ios - 如何从另一个 ViewController 检索 UseDefault 保存的数据

ios - 用于 Paypal token 的 Paypal iOS SDK