J'ai besoin de l'indicateur d'activité pour démarrer chaque fois qu'une requête Web est effectuée et qu'elle s'arrête une fois l'opération terminée. À l'heure actuelle, l'indicateur s'affiche, mais le .stopAnimation
s'exécute même immédiatement et n'attend pas la fin de la requête Web.Impossible d'arrêter l'indicateur d'activité après une requête Web asynchrone
import UIKit
class ViewController:
UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {
@IBOutlet weak var locationPickerOutlet: UIPickerView!
@IBOutlet weak var theProgressOutlet: UIActivityIndicatorView!
var locationspickerData: [String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
populateLocations()
}
func populateLocations() {
self.locationspickerData = ["All Locations"]
let url:URL = URL(string: "http://<web address>")!
let session = URLSession.shared
let request = NSMutableURLRequest(url: url)
request.httpMethod = "GET"
//request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(data, response, error) in
guard let _:Data = data, let _:URLResponse = response , error == nil else {
return
}
let json: Any?
do
{
json = try JSONSerialization.jsonObject(with: data!, options: [])
}
catch
{
return
}
guard let data_list = json as? NSArray else
{
return
}
if let locations_list = json as? NSArray
{
for i in 0 ..< data_list.count
{
if let locations_obj = locations_list[i] as? NSDictionary
{
if let location_name = locations_obj["name"] as? String
{
self.locationspickerData.append(location_name)
}
}
}
self.locationPickerOutlet.delegate = self
self.locationPickerOutlet.dataSource = self
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
self.theProgressOutlet.stopAnimating()
}
}
})
task.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return locationspickerData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return locationspickerData[row]
}
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let titleData = locationspickerData[row]
let myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 15.0)!,NSForegroundColorAttributeName:UIColor.blue])
return myTitle
}
Peut-être que votre requête Web est que rapide ;-). Btw: Considérez que l'indicateur ne s'arrêtera pas en cas d'erreur. Ce serait un bon exemple pour l'instruction 'defer'. – vadian
PS: ... et le conseil habituel: N'utilisez pas de 'NSMutable ...' s'il y a une contrepartie natif Swift et non pas 'NSArray',' NSDictionary' dans Swift non plus. – vadian
quelle alternative existe-t-il pour NSDictionary? – t2nator