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.
This commit is contained in:
Shadowfacts 2020-01-19 23:11:47 -05:00
parent 8eb6f6f573
commit 2f630f2f8f
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 10 additions and 1 deletions

View File

@ -20,7 +20,7 @@ class MastodonCache {
let statusSubject = PassthroughSubject<Status, Never>()
let accountSubject = PassthroughSubject<Account, Never>()
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,