首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Swift中使用URLSession添加UIActivityIndicatorView?

如何在Swift中使用URLSession添加UIActivityIndicatorView?
EN

Stack Overflow用户
提问于 2019-01-04 09:08:55
回答 1查看 566关注 0票数 0

我有一个Master-Detail项目,我在该项目中解析来自JSON的数据。其目的是在等待数据获取并加载到DetailsViewController时,将UIActivityIndicatorView (通过使用URLSession)添加到DetailsViewController。我尝试了几种方法,在主控中启动UIActivityIndicatorView,如下所示:

代码语言:javascript
复制
    let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in

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

代码语言:javascript
复制
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    configureView()
}

但也没有起作用。我在任何地方都找不到使用URLSession的状态添加活动指示器的信息。在这里,我添加了MasterViewController中的代码,我尝试在其中启动活动指示器:

代码语言:javascript
复制
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()
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-04 14:04:51

创建NetworkService类并在函数中进行api调用,这种方式会更好。

代码语言:javascript
复制
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()
}

}

}

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

代码语言:javascript
复制
  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
   }
 }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54031942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档