http - swift uploadTaskWithRequest with didReceiveData -


i pretty new swift have following code

    var data : anyobject       let dict = jsonobject nsdictionary         {         data = try nsjsonserialization.datawithjsonobject(dict, options:.prettyprinted)          let strdata = nsstring(data: data as! nsdata, encoding: nsutf8stringencoding)! string          data = strdata.datausingencoding(nsutf8stringencoding)!           let task = defaultsession.uploadtaskwithrequest(request, fromdata: data as? nsdata,             completionhandler:             {(data,response,error) in                  guard let _:nsdata = data, let _:nsurlresponse = response  error == nil                 else {                     return                 }      });      task.resume()  }catch{      return resultjson } 

the resultjson object returns empty array there more date downloaded , takes time.i wondering weather can use didreceivedata option return data after downloaded. searched code online couldn't find any. code appreciated.

thanks

you're right in return run before upload finished, , therefore won't desired result. assume code in function. need change take closure parameter. when upload finished, call closure. like:

func dotheupload(completion completionhandler: ((anyobject?) -> void)) {     var data : anyobject     let dict = jsonobject nsdictionary         {         data = try nsjsonserialization.datawithjsonobject(dict, options:.prettyprinted)         let strdata = nsstring(data: data as! nsdata, encoding: nsutf8stringencoding)! string         data = strdata.datausingencoding(nsutf8stringencoding)!         let task = defaultsession.uploadtaskwithrequest(request, fromdata: data as? nsdata,             completionhandler:             {(data,response,error) in                 guard let _:nsdata = data, let _:nsurlresponse = response  error == nil                     completionhandler(resultjson)                 else {                     return                 }         });         task.resume()     }catch{         // error     } } 

then call as:

dotheupload(completion: {     resultjson in     // use result }) 

note don't know you're getting resultjson or type is, have make changes.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -