Skip to content

Instantly share code, notes, and snippets.

@jsoneaday
Created October 30, 2020 17:14
Show Gist options
  • Save jsoneaday/39f88aea7c84fd8a16d5ca9b488bffb8 to your computer and use it in GitHub Desktop.
Save jsoneaday/39f88aea7c84fd8a16d5ca9b488bffb8 to your computer and use it in GitHub Desktop.
Weak on URLSession in use
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!) { [weak self] (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