From 0a16a2e2618a864109f6fd898b79c0b50f3d1e70 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 26 Jun 2021 16:51:54 -0400 Subject: [PATCH] Fix potential data races --- Tusker/Screens/Profile/ProfileViewController.swift | 2 +- Tusker/Views/Attachments/AttachmentView.swift | 4 +++- .../Views/Profile Header/ProfileHeaderView.swift | 14 ++++++++++---- Tusker/Views/Status/BaseStatusTableViewCell.swift | 4 ++-- .../Views/Status/TimelineStatusTableViewCell.swift | 2 +- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Tusker/Screens/Profile/ProfileViewController.swift b/Tusker/Screens/Profile/ProfileViewController.swift index 7bd58502..246c4924 100644 --- a/Tusker/Screens/Profile/ProfileViewController.swift +++ b/Tusker/Screens/Profile/ProfileViewController.swift @@ -91,8 +91,8 @@ class ProfileViewController: UIPageViewController { addKeyCommand(MenuController.nextSubTabCommand) accountUpdater = mastodonController.persistentContainer.accountSubject - .filter { [weak self] in $0 == self?.accountID } .receive(on: DispatchQueue.main) + .filter { [weak self] in $0 == self?.accountID } .sink { [weak self] (_) in self?.updateAccountUI() } loadAccount() diff --git a/Tusker/Views/Attachments/AttachmentView.swift b/Tusker/Views/Attachments/AttachmentView.swift index efb49ab9..d23b0098 100644 --- a/Tusker/Views/Attachments/AttachmentView.swift +++ b/Tusker/Views/Attachments/AttachmentView.swift @@ -164,7 +164,9 @@ class AttachmentView: UIImageView, GIFAnimatable { let attachmentURL = attachment.url attachmentRequest = ImageCache.attachments.get(attachmentURL) { [weak self] (data, _) in guard let self = self, let data = data else { return } - self.attachmentRequest = nil + DispatchQueue.main.async { + self.attachmentRequest = nil + } if self.attachment.url.pathExtension == "gif" { self.source = .gifData(attachmentURL, data) if self.autoplayGifs { diff --git a/Tusker/Views/Profile Header/ProfileHeaderView.swift b/Tusker/Views/Profile Header/ProfileHeaderView.swift index 700d9666..29c52422 100644 --- a/Tusker/Views/Profile Header/ProfileHeaderView.swift +++ b/Tusker/Views/Profile Header/ProfileHeaderView.swift @@ -80,14 +80,14 @@ class ProfileHeaderView: UIView { cancellables = [] mastodonController.persistentContainer.accountSubject - .filter { [weak self] in $0 == self?.accountID } .receive(on: DispatchQueue.main) + .filter { [weak self] in $0 == self?.accountID } .sink { [weak self] in self?.updateUI(for: $0) } .store(in: &cancellables) mastodonController.persistentContainer.relationshipSubject - .filter { [weak self] in $0 == self?.accountID } .receive(on: DispatchQueue.main) + .filter { [weak self] in $0 == self?.accountID } .sink { [weak self] (_) in self?.updateRelationship() } .store(in: &cancellables) } @@ -203,11 +203,14 @@ class ProfileHeaderView: UIView { let image = image, self.accountID == accountID, let transformedImage = ImageGrayscalifier.convertIfNecessary(url: avatarURL, image: image) else { + DispatchQueue.main.async { + self?.avatarRequest = nil + } return } - self.avatarRequest = nil DispatchQueue.main.async { + self.avatarRequest = nil self.avatarImageView.image = transformedImage } } @@ -217,11 +220,14 @@ class ProfileHeaderView: UIView { let image = image, self.accountID == accountID, let transformedImage = ImageGrayscalifier.convertIfNecessary(url: header, image: image) else { + DispatchQueue.main.async { + self?.headerRequest = nil + } return } - self.headerRequest = nil DispatchQueue.main.async { + self.headerRequest = nil self.headerImageView.image = transformedImage } } diff --git a/Tusker/Views/Status/BaseStatusTableViewCell.swift b/Tusker/Views/Status/BaseStatusTableViewCell.swift index 281d28df..0af7cd84 100644 --- a/Tusker/Views/Status/BaseStatusTableViewCell.swift +++ b/Tusker/Views/Status/BaseStatusTableViewCell.swift @@ -106,8 +106,8 @@ class BaseStatusTableViewCell: UITableViewCell, MenuPreviewProvider { open func createObserversIfNecessary() { if statusUpdater == nil { statusUpdater = mastodonController.persistentContainer.statusSubject - .filter { [unowned self] in $0 == self.statusID } .receive(on: DispatchQueue.main) + .filter { [unowned self] in $0 == self.statusID } .sink { [unowned self] in if let mastodonController = mastodonController, let status = mastodonController.persistentContainer.status(for: $0) { @@ -118,8 +118,8 @@ class BaseStatusTableViewCell: UITableViewCell, MenuPreviewProvider { if accountUpdater == nil { accountUpdater = mastodonController.persistentContainer.accountSubject - .filter { [unowned self] in $0 == self.accountID } .receive(on: DispatchQueue.main) + .filter { [unowned self] in $0 == self.accountID } .sink { [unowned self] in if let mastodonController = mastodonController, let account = mastodonController.persistentContainer.account(for: $0) { diff --git a/Tusker/Views/Status/TimelineStatusTableViewCell.swift b/Tusker/Views/Status/TimelineStatusTableViewCell.swift index 5e6c3508..9d43650d 100644 --- a/Tusker/Views/Status/TimelineStatusTableViewCell.swift +++ b/Tusker/Views/Status/TimelineStatusTableViewCell.swift @@ -59,8 +59,8 @@ class TimelineStatusTableViewCell: BaseStatusTableViewCell { if rebloggerAccountUpdater == nil { rebloggerAccountUpdater = mastodonController.persistentContainer.accountSubject - .filter { [unowned self] in $0 == self.rebloggerID } .receive(on: DispatchQueue.main) + .filter { [unowned self] in $0 == self.rebloggerID } .sink { [unowned self] in if let mastodonController = self.mastodonController, let reblogger = mastodonController.persistentContainer.account(for: $0) {