From 2f630f2f8fffe548dafbba2c0e83ab36aacf2db9 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 19 Jan 2020 23:11:47 -0500 Subject: [PATCH] Fix retain cycle between MastodonController/MastodonCache The cache should only store a weak reference to the controller, so that when the controller is deinit'd the cache is as well. --- Tusker/MastodonCache.swift | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Tusker/MastodonCache.swift b/Tusker/MastodonCache.swift index 0c22b4e2..9aec17c6 100644 --- a/Tusker/MastodonCache.swift +++ b/Tusker/MastodonCache.swift @@ -20,7 +20,7 @@ class MastodonCache { let statusSubject = PassthroughSubject() let accountSubject = PassthroughSubject() - let mastodonController: MastodonController + weak var mastodonController: MastodonController? init(mastodonController: MastodonController) { self.mastodonController = mastodonController @@ -43,6 +43,9 @@ class MastodonCache { } func status(for id: String, completion: @escaping (Status?) -> Void) { + guard let mastodonController = mastodonController else { + fatalError("The MastodonController for this cache has been deinitialized, so this cache should no longer exist. Are you storing a strong reference to it?") + } let request = Client.getStatus(id: id) mastodonController.run(request) { response in guard case let .success(status, _) = response else { @@ -73,6 +76,9 @@ class MastodonCache { } func account(for id: String, completion: @escaping (Account?) -> Void) { + guard let mastodonController = mastodonController else { + fatalError("The MastodonController for this cache has been deinitialized, so this cache should no longer exist. Are you storing a strong reference to it?") + } let request = Client.getAccount(id: id) mastodonController.run(request) { response in guard case let .success(account, _) = response else { @@ -102,6 +108,9 @@ class MastodonCache { } func relationship(for id: String, completion: @escaping (Relationship?) -> Void) { + guard let mastodonController = mastodonController else { + fatalError("The MastodonController for this cache has been deinitialized, so this cache should no longer exist. Are you storing a strong reference to it?") + } let request = Client.getRelationships(accounts: [id]) mastodonController.run(request) { response in guard case let .success(relationships, _) = response,