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