1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
// preparation de la requete
let urlsite=URL_BASE
let url=NSURL(string: urlsite)!
let request=NSMutableURLRequest(URL: url)
request.HTTPMethod="POST"
// lancement de la requete
let session=NSURLSession.sharedSession()
let task=session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
if error != nil {
if error!.domain == NSURLErrorDomain && error!.code == NSURLErrorTimedOut {
print("[ARTICLE] : timed out")
}
}
else
{
let json=JSON(data: data!)
self.appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
self.context=self.appDelegate!.managedObjectContext
print("[ARTICLE] : Download OK")
var val:Float=0
var cpt:Int=0
var oldcpt:Int=0
var nb:Int=0
if json["retour"] == "OK" {
print("[ARTICLE] : \(json["article"].count) articles")
for (_,subJson):(String, JSON) in json["article"] {
let art:Article=NSEntityDescription.insertNewObjectForEntityForName("Article", inManagedObjectContext: self.context!) as! Article
art.idarticle=subJson[0].intValue
art.designation=subJson[1].stringValue
cpt=Int(Float(nb)/Float(json["article"].count)*Float(100))
if cpt != oldcpt {
oldcpt=cpt
val=Float(nb)/Float(json["article"].count)
if let delegate = self.delegate {
delegate.DownloadProgression!(val)
}
}
nb++
}
}
self.appDelegate!.saveContext()
} |