Avatar
Kishikawa Katsumi 5/15/2020 1:05 PM
import UIKit import Combine class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() DispatchQueue.main.asyncAfter(deadline: .now() + 2) { [weak self] in let vc = ViewController2() self?.present(vc, animated: true) } } } class ViewController2: UIViewController { private var cancellables: Set<AnyCancellable> = [] deinit { print("deinit") } override func viewDidLoad() { super.viewDidLoad() Client.shared.perform(request: URLRequest(url: URL(string: "https://www.google.com")!)) .sink(receiveCompletion: { (completion) in print(completion) self.updateUI() }) { (response) in print(response) self.updateUI() } .store(in: &cancellables) } func updateUI() { } } class Client { static let shared = Client() private let session = URLSession(configuration: .default) private init() {} func perform(request: URLRequest) -> Future<URLResponse, Error> { return Future<URLResponse, Error> { [weak self] promise in self?.session.dataTask(with: request) { (data, response, error) in if let error = error { promise(.failure(error)) return } if let response = response { promise(.success(response)) return } } .resume() } } }
1:06 PM
^ 小さく再現できるコードだとこうなるんですけど、これはViewController2の self.updateUI() の呼び出しはweak selfじゃないとViewController2を閉じてもViewController2は解放されないんですよね。