Created
October 30, 2020 17:01
-
-
Save jsoneaday/cbe252aae0c1b12bb274d943f940c1f0 to your computer and use it in GitHub Desktop.
Weak on URLSession
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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