From 8f9cf96f3d7ac364940893c8208fb8608db99700 Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Wed, 24 May 2023 19:13:40 -0500 Subject: [PATCH] Fixes strong reference cycles in Source view controllers * SourcesViewController * SourcesDetailContentViewController --- .../SourceDetailContentViewController.swift | 6 +++--- AltStore/Sources/SourcesViewController.swift | 18 ++++++++++-------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/AltStore/Sources/SourceDetailContentViewController.swift b/AltStore/Sources/SourceDetailContentViewController.swift index 9957d501..5c52fe5b 100644 --- a/AltStore/Sources/SourceDetailContentViewController.swift +++ b/AltStore/Sources/SourceDetailContentViewController.swift @@ -293,12 +293,12 @@ private extension SourceDetailContentViewController { let dataSource = RSTDynamicCollectionViewDataSource() dataSource.numberOfSectionsHandler = { 1 } - dataSource.numberOfItemsHandler = { _ in self.source.localizedDescription == nil ? 0 : 1 } + dataSource.numberOfItemsHandler = { [source] _ in source.localizedDescription == nil ? 0 : 1 } dataSource.cellIdentifierHandler = { _ in "AboutCell" } - dataSource.cellConfigurationHandler = { [weak self] (cell, _, indexPath) in + dataSource.cellConfigurationHandler = { [source] (cell, _, indexPath) in let cell = cell as! TextViewCollectionViewCell cell.contentView.layoutMargins = .zero // Fixes incorrect margins if not initially on screen. - cell.textView.text = self?.source.localizedDescription + cell.textView.text = source.localizedDescription cell.textView.isCollapsed = false } diff --git a/AltStore/Sources/SourcesViewController.swift b/AltStore/Sources/SourcesViewController.swift index f42d9ee6..670393fa 100644 --- a/AltStore/Sources/SourcesViewController.swift +++ b/AltStore/Sources/SourcesViewController.swift @@ -120,7 +120,9 @@ private extension SourcesViewController { let dataSource = RSTCompositeCollectionViewDataSource(dataSources: [self.addedSourcesDataSource, self.trustedSourcesDataSource]) dataSource.proxy = self - dataSource.cellConfigurationHandler = { (cell, source, indexPath) in + dataSource.cellConfigurationHandler = { [weak self] (cell, source, indexPath) in + guard let self else { return } + let tintColor = UIColor.altPrimary let cell = cell as! AppBannerCollectionViewCell @@ -337,9 +339,9 @@ private extension SourcesViewController func fetchTrustedSources() { - func finish(_ result: Result<[Source], Error>) - { - self.fetchTrustedSourcesResult = result.map { _ in () } + // Closure instead of local function so we can capture `self` weakly. + let finish: (Result<[Source], Error>) -> Void = { [weak self] result in + self?.fetchTrustedSourcesResult = result.map { _ in () } DispatchQueue.main.async { do @@ -348,19 +350,19 @@ private extension SourcesViewController print("Fetched trusted sources:", sources.map { $0.identifier }) let sectionUpdate = RSTCellContentChange(type: .update, sectionIndex: 0) - self.trustedSourcesDataSource.setItems(sources, with: [sectionUpdate]) + self?.trustedSourcesDataSource.setItems(sources, with: [sectionUpdate]) } catch { print("Error fetching trusted sources:", error) let sectionUpdate = RSTCellContentChange(type: .update, sectionIndex: 0) - self.trustedSourcesDataSource.setItems([], with: [sectionUpdate]) + self?.trustedSourcesDataSource.setItems([], with: [sectionUpdate]) } } } - self.fetchTrustedSourcesOperation = AppManager.shared.updateKnownSources { result in + self.fetchTrustedSourcesOperation = AppManager.shared.updateKnownSources { [weak self] result in switch result { case .failure(let error): finish(.failure(error)) @@ -370,7 +372,7 @@ private extension SourcesViewController // This context is never saved, but keeps the managed sources alive. let context = DatabaseManager.shared.persistentContainer.newBackgroundSavingViewContext() - self._fetchTrustedSourcesContext = context + self?._fetchTrustedSourcesContext = context let dispatchGroup = DispatchGroup()