Skip to content

Swift 6: complete concurrency checking for StreamChatUI #3660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 18 commits into
base: swift-6
Choose a base branch
from

Conversation

laevandus
Copy link
Contributor

@laevandus laevandus commented May 5, 2025

Important

Merges to the main swift-6 branch, not develop

🔗 Issue Links

Resolves IOS-735

🎯 Goal

  • Set Swift version to 6 and implement complete concurrency checking in StreamChatUI

📝 Summary

  • Completion blocks need to be @Sendable in most of the cases
  • Controller completions and delegates must ensure main thread (through custom MainActor.ensureIsolated)
  • Controller delegates get nonisolated because any thread thread could call it and often MainActor guarded type implements these delegates (e.g. UIViewController subclass)
  • Tests use nonisolated(unsafe) a lot because otherwise most of the test must be rewritten (huge effort)
  • Sendable conformance to many types
  • Many protocols and types related to UI get @MainActor requirement
  • Upgrade Nuke to version 12.8 (otherwise we can't compile with complete concurrency checking)
  • Patch SwiftyGif for supporting complete concurrency checking

🛠 Implementation

LLC controllers support any queue for delegate callbacks and completion handlers. Therefore we need to ensure that these run on the main thread when updating UI code. This is where the custom MainActor.ensureIsolated(_) comes into the play. It just ensures that code triggered from these code paths are always on the main thread.

Public API which requires MainActor are annotated with @preconcurrency @MainActor which is the backwards compatibility support. In the next major version we would clean it up.

🧪 Manual Testing Notes

Manual testing in #3661

☑️ Contributor Checklist

  • I have signed the Stream CLA (required)
  • This change should be manually QAed
  • Changelog is updated with client-facing changes
  • Changelog is updated with new localization keys
  • New code is covered by unit tests
  • Documentation has been updated in the docs-content repo

# Conflicts:
#	Tests/StreamChatUITests/SnapshotTests/ChatChannelList/Search/ChatMessageSearchVC_Tests.swift
#	Tests/StreamChatUITests/SnapshotTests/ChatMessageList/ChatMessage/ChatMessageMarkdown_Tests.swift
@laevandus laevandus added 🎨 SDK: StreamChatUI Tasks related to the StreamChatUI SDK ⏫ Dependencies Update A PR that updates SDK Dependencies ✅ Feature An issue or PR related to a feature labels May 5, 2025
@laevandus laevandus requested a review from a team as a code owner May 5, 2025 07:39
@laevandus laevandus marked this pull request as draft May 5, 2025 07:39
Copy link

github-actions bot commented May 5, 2025

1 Message
📖 Skipping Danger since the Pull Request is classed as Draft/Work In Progress

Generated by 🚫 Danger

@Stream-SDK-Bot
Copy link
Collaborator

SDK Size

title develop branch diff status
StreamChat 7.2 MB 7.28 MB +81 KB 🟢
StreamChatUI 4.72 MB 4.77 MB +55 KB 🟢

@@ -15,7 +15,7 @@ open class DefaultChannelNameFormatter: ChannelNameFormatter {
public init() {}

/// Internal static property to add backwards compatibility to `Components.channelNamer`
internal static var channelNamer: (
nonisolated(unsafe) internal static var channelNamer: (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Review if MainActor is more approriate here

@Stream-SDK-Bot
Copy link
Collaborator

SDK Performance

target metric benchmark branch performance status
MessageList Hitches total duration 10 ms 10.01 ms -0.1% 🔽 🟡
Duration 2.6 s 2.54 s 2.31% 🔼 🟢
Hitch time ratio 4 ms per s 3.94 ms per s 1.5% 🔼 🟢
Frame rate 75 fps 78.37 fps 4.49% 🔼 🟢
Number of hitches 1 1.2 -20.0% 🔽 🔴

Comment on lines 42 to 48
static var `default`: Appearance = .init()
static var `default`: Appearance {
get { queue.sync { _default } }
set { queue.sync { _default = newValue } }
}

private static let queue = DispatchQueue(label: "io.getstream.appearance", target: .global())
nonisolated(unsafe) private static var _default: Appearance = .init()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the second look it actually does not feel like a good idea. Since this is used for UI code then it sounds like making the static property MainActor makes more sense here. Otherwise we do unnecessary context switches. Making it main actor will have consequences:

  • app level setup code must be done on the main thread
  • other code interacting with default must require main thread

Copy link
Contributor Author

@laevandus laevandus May 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, options are:
a) opt-out with nonisolated(unsafe) which kind of loses the point of strict concurrency checking. SDK users can trigger crashes.
b) Internally uses main thread when accessing the default (which should be the 99% of cases when calling it)

    static var `default`: Appearance {
        get {
            MainActor.ensureIsolated { _default }
        }
        set {
            MainActor.ensureIsolated { _default = newValue }
        }
    }

and making Appearance @unchecked Sendable (lot of work to make it fully Sendable since this type captures so many other types, e.g formatters)
c) Make the default @MainActor and add MainActor requirement to all the other callsites
e) Use queue/lock for accessing the default state although it should (almost) always be main thread

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a) loses the point of concurrency checking
b) makes it easier to use default from places without main actor annotation (easier adoption), on the other hand, it does not look the nicest, but at least does not cause performance issues due to context switches
c) cleanest but harder to use for users since it forces MainActor
e) performance wise not good because this is used by UI code and should be almost always run on the main thread (that said, SDK users could have setup running on a background thread)

self.configurable = configurable
self.defaultValue = defaultValue
}
}

extension PollsEntryConfig {
/// The default configuration for a poll entry. It will make it configurable but disabled by default.
public static let `default` = PollsEntryConfig(configurable: true, defaultValue: false)
// TODO: Uncomment later
public static var `default`: PollsEntryConfig { PollsEntryConfig(configurable: true, defaultValue: false) }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why, but compiler crashes with Xcode 15 when using default for init above. The only way I found was to skip using default and set the values in the PollEntryConfig. Compiles fine with Xcode 16.

[dateView, unreadCountView].forEach(container.addArrangedSubview)
[dateView, unreadCountView].forEach { container.addArrangedSubview($0) }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Xcode 15 fails to compile such code when complete concurrency checking is enabled (stack was not useful) OK with Xcode 16.

) {
self.listView = listView
self.impactFeedbackGenerator = impactFeedbackGenerator
self.impactFeedbackGenerator = impactFeedbackGenerator ?? UIImpactFeedbackGenerator(style: .medium)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Xcode 15 had compiler crashes and this was the only way I could get it compiling

@Stream-SDK-Bot
Copy link
Collaborator

Stream-SDK-Bot commented May 5, 2025

SDK Size

title develop branch diff status
StreamChat 7.2 MB 7.28 MB +81 KB 🟢
StreamChatUI 4.72 MB 4.8 MB +87 KB 🟢

Comment on lines -42 to +53
static var `default`: Appearance = .init()
static var `default`: Appearance {
get {
MainActor.ensureIsolated { _default }
}
set {
MainActor.ensureIsolated { _default = newValue }
}
}

// Shared instance is mutated only on the main thread without explicit
// main actor annotation for easier SDK setup.
nonisolated(unsafe) private static var _default: Appearance = .init()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first I was thinking about making it @MainActor, but this is used in multiple init methods meaning all of these must be @MainActor as well. Moreover, it might make the SDK initialization more complex as well since SDK users need to make sure Appearance is changed only from the MainActor.
a) we just make default nonisolated(unsafe)
b) we do what we have above which internally forcing main actor (for making sure SDK users do not corrupt the default by changing it from background threads)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⏫ Dependencies Update A PR that updates SDK Dependencies ✅ Feature An issue or PR related to a feature 🎨 SDK: StreamChatUI Tasks related to the StreamChatUI SDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants