我有一个Master-Detail项目,我在该项目中解析来自JSON的数据。其目的是在等待数据获取并加载到DetailsViewController时,将UIActivityIndicatorView (通过使用URLSession)添加到DetailsViewController。我尝试了几种方法,在主控中启动UIActivityIndicatorView,如下所示:
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()
}
}发布于 2019-01-04 14:04:51
创建NetworkService类并在函数中进行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
}
}https://stackoverflow.com/questions/54031942
复制相似问题