Mohit Gurumukhani
07/10/2018, 3:01 AMfun call() : Unit{
var url = NSURL.alloc()
url = url?.initWithString("<https://jsonplaceholder.typicode.com/posts/1>")
var nsrequest = NSMutableURLRequest(url as NSURL)
nsrequest.HTTPMethod = "GET"
var this_nsdata : NSData? = null
var this_nsresponse : NSURLResponse? = null
var this_nserror : NSError? = null
var syncer = dispatch_semaphore_create(0)
var sess = NSURLSession.sharedSession()
var task = sess.dataTaskWithRequest(nsrequest) { nsdata: NSData?, nsresponse: NSURLResponse?, nserror: NSError? ->
this_nsdata = nsdata
this_nsresponse = nsresponse as NSHTTPURLResponse
this_nserror = nserror
dispatch_semaphore_signal(syncer)
}
task.resume()
dispatch_semaphore_wait(syncer, DISPATCH_TIME_FOREVER)
if(this_nserror == null && this_nsdata != null){
this_nsresponse as NSHTTPURLResponse
var nsstrobj = NSString.stringWithUTF8String(this_nsdata!!.bytes as CPointer<ByteVar>)
print(nsstrobj as String)
}
else {
print("whoops")
}
}
olonho
07/10/2018, 6:11 AMthevery
07/10/2018, 7:10 AMthevery
07/10/2018, 7:11 AMspierce7
07/10/2018, 5:52 PMspierce7
07/10/2018, 6:55 PMthevery
07/10/2018, 7:15 PMthevery
07/10/2018, 7:17 PM{ nsdata: NSData?, nsresponse: NSURLResponse?, nserror: NSError? ->
lambda is invoked in bg thread -> thread crossing triggers immutability violationthevery
07/10/2018, 7:17 PMthevery
07/10/2018, 7:18 PMinitRuntimeIfNeeded
, @olonho should know better than me 😃thevery
07/10/2018, 7:20 PMval body = data?.let { NSString.create(it, NSUTF8StringEncoding) as? String } ?: ""
olonho
07/11/2018, 10:28 AMinitRuntimeIfNeeded
will go away eventually