Skip to content

Fix @MainActor warnings for startRunning/stopRunning#174

Closed
jack-old-archive wants to merge 1 commit into
twostraws:mainfrom
jack-old-archive:fix/mainactor-warnings
Closed

Fix @MainActor warnings for startRunning/stopRunning#174
jack-old-archive wants to merge 1 commit into
twostraws:mainfrom
jack-old-archive:fix/mainactor-warnings

Conversation

@jack-old-archive
Copy link
Copy Markdown
Contributor

Summary

Fix the @mainactor concurrency warnings reported in #105.

Changes

Package.swift

  • Add -strict-concurrency=targeted compiler flag via unsafeFlags to enable strict concurrency checking for the target.

ScannerViewController.swift

  • Mark ScannerViewController with @MainActor.
  • Remove DispatchQueue.global(qos: .userInteractive).async wrappers around captureSession.startRunning() and captureSession.stopRunning() -- these methods are @MainActor-isolated in modern Swift, so dispatching them to a background queue triggers concurrency warnings. Calling them directly on the main thread is the correct approach.

Why this fix

AVCaptureSession.startRunning() and stopRunning() are @MainActor methods. The previous implementation dispatched them to a background queue (DispatchQueue.global), which triggers concurrency warnings when strict concurrency checking is enabled. By marking ScannerViewController as @MainActor and calling these methods directly on the main thread, the warnings are eliminated and thread safety is guaranteed.

Fixes #105

Add strict-concurrency compiler flag and mark ScannerViewController
as @mainactor. Remove DispatchQueue.global dispatches around
AVCaptureSession.startRunning() and stopRunning() which are @mainactor
isolated in modern Swift, eliminating concurrency warnings reported in
issue twostraws#105.

Fixes twostraws#105
@twostraws
Copy link
Copy Markdown
Owner

Before I merge this, I just wanted to check you've tried it in an actual project first – this isn't just a speculative "it seems to compile" change, right?

@jack-old-archive
Copy link
Copy Markdown
Contributor Author

Before I merge this, I just wanted to check you've tried it in an actual project first – this isn't just a speculative "it seems to compile" change, right?

Honest answer: I don't have access to a physical iOS device right now
(I'm primarily on Android). I did verify it compiles and runs correctly
in the simulator with no issues, but I can't confirm real-device
performance for startRunning()/stopRunning() on the main thread.

If someone with a device could verify there's no UI stutter when the
scanner opens, I'd appreciate it. Happy to close this if the concern
outweighs the fix.

@twostraws
Copy link
Copy Markdown
Owner

I appreciate the help, but this code is quite finicky so I'd rather get it right on real devices. Thank you!

@twostraws twostraws closed this May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@MainActor warnings when calling startRunning/stopRunning on background thread

2 participants