From 7eb79414a45607e0fe16fecc6167c83071f03895 Mon Sep 17 00:00:00 2001 From: ihugo Date: Mon, 26 Apr 2021 17:41:24 +0800 Subject: [PATCH] fix: memery leak --- .../Diffiable/Section/ReportSection.swift | 3 ++- .../Diffiable/Section/StatusSection.swift | 26 +++++++++++++------ Mastodon/Scene/Report/ReportViewModel.swift | 8 +++--- .../Report/ReportedStatusTableviewCell.swift | 2 +- .../View/Content/TimelineHeaderView.swift | 2 ++ 5 files changed, 26 insertions(+), 15 deletions(-) diff --git a/Mastodon/Diffiable/Section/ReportSection.swift b/Mastodon/Diffiable/Section/ReportSection.swift index e14a959f7..6faaae6c2 100644 --- a/Mastodon/Diffiable/Section/ReportSection.swift +++ b/Mastodon/Diffiable/Section/ReportSection.swift @@ -36,7 +36,8 @@ extension ReportSection { cell.dependency = dependency let activeMastodonAuthenticationBox = dependency.context.authenticationService.activeMastodonAuthenticationBox.value let requestUserID = activeMastodonAuthenticationBox?.userID ?? "" - managedObjectContext.performAndWait { + managedObjectContext.performAndWait { [weak dependency] in + guard let dependency = dependency else { return } let status = managedObjectContext.object(with: objectID) as! Status StatusSection.configure( cell: cell, diff --git a/Mastodon/Diffiable/Section/StatusSection.swift b/Mastodon/Diffiable/Section/StatusSection.swift index ea3332f14..b897de47f 100644 --- a/Mastodon/Diffiable/Section/StatusSection.swift +++ b/Mastodon/Diffiable/Section/StatusSection.swift @@ -149,7 +149,8 @@ extension StatusSection { .receive(on: DispatchQueue.main) .sink { _ in // do nothing - } receiveValue: { change in + } receiveValue: { [weak cell] change in + guard let cell = cell else { return } guard case .update(let object) = change.changeType, let newStatus = object as? Status else { return } StatusSection.configureHeader(cell: cell, status: newStatus) @@ -221,7 +222,8 @@ extension StatusSection { } else { meta.blurhashImagePublisher() .receive(on: DispatchQueue.main) - .sink { image in + .sink { [weak cell] image in + guard let cell = cell else { return } blurhashOverlayImageView.image = image image?.pngData().flatMap { blurhashImageCache.setObject($0 as NSData, forKey: blurhashImageDataKey) @@ -246,7 +248,8 @@ extension StatusSection { statusItemAttribute.isRevealing ) .receive(on: DispatchQueue.main) - .sink { isImageLoaded, isMediaRevealing in + .sink { [weak cell] isImageLoaded, isMediaRevealing in + guard let cell = cell else { return } guard isImageLoaded else { blurhashOverlayImageView.alpha = 1 blurhashOverlayImageView.isHidden = false @@ -355,7 +358,8 @@ extension StatusSection { .receive(on: DispatchQueue.main) .sink { _ in // do nothing - } receiveValue: { [weak dependency] change in + } receiveValue: { [weak dependency, weak cell] change in + guard let cell = cell else { return } guard let dependency = dependency else { return } guard case .update(let object) = change.changeType, let status = object as? Status else { return } @@ -382,7 +386,8 @@ extension StatusSection { ManagedObjectObserver.observe(object: poll) .sink { _ in // do nothing - } receiveValue: { change in + } receiveValue: { [weak cell] change in + guard let cell = cell else { return } guard case .update(let object) = change.changeType, let newPoll = object as? Poll else { return } StatusSection.configurePoll( @@ -413,7 +418,8 @@ extension StatusSection { let createdAt = (status.reblog ?? status).createdAt cell.statusView.dateLabel.text = createdAt.shortTimeAgoSinceNow timestampUpdatePublisher - .sink { _ in + .sink { [weak cell] _ in + guard let cell = cell else { return } cell.statusView.dateLabel.text = createdAt.shortTimeAgoSinceNow } .store(in: &cell.disposeBag) @@ -423,7 +429,9 @@ extension StatusSection { .receive(on: DispatchQueue.main) .sink { _ in // do nothing - } receiveValue: { change in + } receiveValue: { [weak dependency, weak cell] change in + guard let cell = cell else { return } + guard let dependency = dependency else { return } guard case .update(let object) = change.changeType, let status = object as? Status else { return } StatusSection.configureActionToolBar( @@ -759,7 +767,9 @@ extension StatusSection { } var children: [UIMenuElement] = [] let name = author.displayNameWithFallback - let reportAction = UIAction(title: L10n.Common.Controls.Actions.reportUser(name), image: UIImage(systemName: "exclamationmark.bubble"), identifier: nil, discoverabilityTitle: nil, attributes: [], state: .off) { _ in + let reportAction = UIAction(title: L10n.Common.Controls.Actions.reportUser(name), image: UIImage(systemName: "exclamationmark.bubble"), identifier: nil, discoverabilityTitle: nil, attributes: [], state: .off) { + [weak dependency] _ in + guard let dependency = dependency else { return } let viewModel = ReportViewModel( context: dependency.context, domain: authenticationBox.domain, diff --git a/Mastodon/Scene/Report/ReportViewModel.swift b/Mastodon/Scene/Report/ReportViewModel.swift index b787cf6c7..8631963c5 100644 --- a/Mastodon/Scene/Report/ReportViewModel.swift +++ b/Mastodon/Scene/Report/ReportViewModel.swift @@ -133,13 +133,11 @@ class ReportViewModel: NSObject { } .store(in: &disposeBag) - input.comment.assign( - to: \.comment, - on: self - ) - .store(in: &disposeBag) input.comment.sink { [weak self] (comment) in guard let self = self else { return } + + self.comment = comment + let sendEnable = (comment?.length ?? 0) > 0 self.sendEnableSubject.send(sendEnable) } diff --git a/Mastodon/Scene/Report/ReportedStatusTableviewCell.swift b/Mastodon/Scene/Report/ReportedStatusTableviewCell.swift index 3cbfface5..b1d0af6b0 100644 --- a/Mastodon/Scene/Report/ReportedStatusTableviewCell.swift +++ b/Mastodon/Scene/Report/ReportedStatusTableviewCell.swift @@ -17,7 +17,7 @@ final class ReportedStatusTableViewCell: UITableViewCell, StatusCell { static let bottomPaddingHeight: CGFloat = 10 - var dependency: ReportViewController? + weak var dependency: ReportViewController? var disposeBag = Set() var pollCountdownSubscription: AnyCancellable? var observations = Set() diff --git a/Mastodon/Scene/Share/View/Content/TimelineHeaderView.swift b/Mastodon/Scene/Share/View/Content/TimelineHeaderView.swift index b5e4c5bde..f095f6f44 100644 --- a/Mastodon/Scene/Share/View/Content/TimelineHeaderView.swift +++ b/Mastodon/Scene/Share/View/Content/TimelineHeaderView.swift @@ -5,6 +5,8 @@ // Created by MainasuK Cirno on 2021-4-6. // +import UIKit + final class TimelineHeaderView: UIView { let iconImageView: UIImageView = {