my code get request this:
dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), { () -> void in // ... let task = nsurlsession.sharedsession().datataskwithrequest(request) { data, response, error in if error != nil { println("error = \(error)") return } if let httpresponse = response as? nshttpurlresponse { if httpresponse.statuscode == 200 { // got response var err: nserror? if let json = nsjsonserialization.jsonobjectwithdata(data!, options: nil, error: &err) as? [string : anyobject] { // success decoding json } else { // failed -> stop activity indicator dispatch_async(dispatch_get_main_queue(), { () -> void in self.activityindicator.stopanimating() }) } } task.resume() }) } } if viewwilldisappear() gets called before request finishes, want stop request.
right now, seems view doesn't disappear before request finishes. there way cancel ongoing get/post request?
yes, have store outside access - task has method cancel() can use, you're using resume().
for viewdiddisappear() i'd recommend having object property - var currenttask: nsurlsessiontask?, in dispatch have self.currenttask = ... instead of let task = ... , in viewdiddisappear() call self.currenttask?.cancel().
Comments
Post a Comment