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

How to add UIActivityIndicatorView by using URLSession in Swift?

我有一个 Master-Detail 项目,我在其中解析来自 JSON 的数据。目的是在等待数据被获取并加载到 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 的状态添加 activity 指标的信息。在这里,我添加了 MasterViewController 中的代码,我在其中尝试启动 activity 指标:

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 class 并在 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()
}

}

}

在视图控制器中调用你的函数:

  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
   }
 }