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

时间:2019-01-04 01:08:55

标签: ios json swift uiactivityindicatorview urlsession

我有一个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的状态添加活动指示器的信息。在这里,我从尝试启动活动指示器的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()
    }
}

1 个答案:

答案 0 :(得分:0)

创建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()
}

}

}

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

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