Skip to content

Instantly share code, notes, and snippets.

@jsoneaday
Created October 30, 2020 17:01
Show Gist options
  • Save jsoneaday/cbe252aae0c1b12bb274d943f940c1f0 to your computer and use it in GitHub Desktop.
Save jsoneaday/cbe252aae0c1b12bb274d943f940c1f0 to your computer and use it in GitHub Desktop.
Weak on URLSession
import Foundation
class MyApiCaller {
var gotData: Bool = false
func getTodos() -> URLSessionDataTask {
let url = URL(string: "https://jsonplaceholder.typicode.com/todos")
let result: URLSessionDataTask = URLSession.shared.dataTask(with: url!) { (data, resp, err) in
guard let todos = data else {
print("No data")
return
}
self.gotData = true
print("Got data \(self.gotData) \(todos)")
}
return result
}
deinit {
print("MyApiCaller is being deinitialized")
}
}
var caller: MyApiCaller? = MyApiCaller()
var result = caller?.getTodos()
caller = nil
result?.resume()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment