Skip to content

pasteboard: read and write the iOS image off the main thread - #559

Merged
boyan01 merged 1 commit into
MixinNetwork:mainfrom
stanbav:pasteboard-ios-image-off-main-thread
Nov 16, 2026
Merged

boyan01 merged 1 commit into
MixinNetwork:mainfrom
stanbav:pasteboard-ios-image-off-main-thread

Conversation

@stanbav

@stanbav stanbav commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Summary

On iOS, Pasteboard.image read UIPasteboard.general.image and encoded the PNG on the main thread. Reading the pasteboard can block while it waits for the data, and the app froze for that long. We saw 3–6 s hangs on iPad with the main thread in -[_UIConcretePasteboard image]; we don't know what the read was waiting for.

image and writeImage now run on one serial queue and reply on the main thread. They return the same data as before and stay in order with each other. UIPasteboard and UIImage are NS_SWIFT_SENDABLE in the SDK headers.

text, writeText and Flutter's Clipboard still run on the main thread, so they can now run ahead of an image or writeImage call that wasn't awaited. The Dart docs of image and writeImage say to await them when the order matters.

Evidence

iOS 27.1 simulator, a test app running the old and the new handler code:

  • 38 pasteboard contents (one and several items; PNG, JPEG, GIF, TIFF, HEIC; EXIF orientation, Retina scale, corrupt data): byte-identical results, each delivered once on the main thread.
  • image followed at once by writeImage, 30 rounds: the earlier image every time, before and after.
  • A second app whose image takes 8 s to arrive, reader allowed to paste:
    Before: main thread blocked 2.8–4.0 s
    After: main thread responsive (longest gap 0.1 s)
    Both returned null. Timing-dependent: in some runs nothing blocked in either version.

Not tested: a physical device, iOS before 27, Universal Clipboard, and the Allow Paste prompt, which the simulator can't show.

Merge Danger

Door: two-way

Blast Radius: small

iOS only, no API change. Code that doesn't await image or writeImage before a text clipboard call can see a different order. macOS still reads on the main thread and is unchanged here.

On iOS, Pasteboard.image read and PNG-encoded the pasteboard image on the
main thread, so the app froze while the pasteboard waited for its data. We
saw 3-6 s hangs on iPad with the main thread inside
-[_UIConcretePasteboard image].

Pasteboard.image and Pasteboard.writeImage now run on one background serial
queue and complete on the main thread. They stay in order with each other.
Pasteboard.text, Pasteboard.writeText and Flutter's Clipboard still run on the
main thread, so await image and writeImage before calling them if the order
matters. A main-thread pasteboard call made while a read is still waiting for
its data still waits.
@boyan01
boyan01 merged commit 471d9da into MixinNetwork:main Nov 16, 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.

2 participants