ios - Variable scoping in Swift with tasks -
i've got of code inside of function in view controller:
self.activityindicator.startanimating() let url = nsurl(string: "http://www.google.com") var request = nsmutableurlrequest(url: url!) request.httpmethod = "put" var err: nserror? request.httpbody = nsjsonserialization.datawithjsonobject(["notes": self.textview.text], options: nil, error: &err) request.addvalue("application/json", forhttpheaderfield: "content-type") request.addvalue("application/json", forhttpheaderfield: "accept") var s = nsstring(data: request.httpbody!, encoding: nsutf8stringencoding) let task = nsurlsession.sharedsession().datataskwithrequest(request, completionhandler: { (data, reponse, error) -> void in // ------------------ // doesn't work // ------------------ self.activityindicator.stopanimating() println(nsstring(data: data, encoding: nsutf8stringencoding)) }) task.resume()
i want activity indicator stop animating when finished task, calling stopanimating() isn't doing - not throwing errors. know doing wrong?
you need on main ui thread this:
// ------------------ // should work :-) // ------------------ dispatch_async(dispatch_get_main_queue(), { // on mainthread self.activityindicator.stopanimating() })
Comments
Post a Comment