Isolate the UIKit-facing API to the main actor - #54
Open
radimvaculik wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
CellKit is a UITableView/UICollectionView data source layer — every one of its protocols is only ever exercised on the main thread. But the protocols are declared nonisolated, so under Swift 6 every consumer conformance reports:
In an app I migrated to Swift 6 this accounted for 61 of 498 warnings — the second largest group. The only consumer-side workaround is to annotate every single conformance:
which had to be repeated 45 times, plus a
@preconcurrency import CellKitwhere a cell model is built off the main thread.Change
@MainActoron the API that touches UIKit:ReusableView,CellConfigurable,CellModel,SupplementaryViewModel,CellConvertible,CellModelSelectable,CellModelDataSourceDelegate,DataSource,CollectionSupplementaryViewModel,AbstractDataSource,CellModelDataSource,LazyCellProvider,LazySupplementaryViewProviderDifferentiableCellModel,DifferentiableCellModelDataSourceDifferentiableCellModelWrapperneeded a small rework: DifferenceKit'sDifferentiable/Equatablerequirements are nonisolated, sodifferenceIdentifieris now computed once in a@MainActor initand stored, and==usesMainActor.assumeIsolated. Wrappers are only ever created and diffed byDifferentiableCellModelDataSource, which is now main actor-isolated, so the assumption holds.MainActor.assumeIsolatedrequires iOS 13, soPackage.swiftplatforms go from.v9to.v13.DiffableCellKitalready required iOS 13 throughout, and current SPM toolchains no longer accept iOS 9 anyway.This is source-breaking
Any conformance that is not main actor-isolated will stop compiling. In practice these types are all cells, cell models and data sources, so this should be a no-op for real consumers — but it is a breaking change and probably warrants a minor version bump.
Verification
xcodebuild buildpasses for both theCellKitandDiffableCellKitschemes ongeneric/platform=iOS. SwiftLint reports the same 9 pre-existing violations asmain— none added.Building
CellKitwithSWIFT_STRICT_CONCURRENCY=completesucceeds; two warnings remain inSupplementaryElementKind, which readsUICollectionView.elementKindSectionHeader/Footerfrom a nonisolatedRawRepresentableconformance. I left it alone because marking the enum@MainActorwould breakRawRepresentable, and the values are immutable strings. Happy to address it separately if you'd prefer.I could not run the app-side end-to-end check (the consumer pins the released tag), so I have not empirically confirmed that all 45
@MainActorannotations become removable — though that is what the change is for.