Repository navigation
pasteboard: read and write the iOS image off the main thread - #559
Merged
boyan01 merged 1 commit intoNov 16, 2026
Merged
Conversation
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.
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.
Summary
On iOS,
Pasteboard.imagereadUIPasteboard.general.imageand 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.imageandwriteImagenow 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.UIPasteboardandUIImageareNS_SWIFT_SENDABLEin the SDK headers.text,writeTextand Flutter'sClipboardstill run on the main thread, so they can now run ahead of animageorwriteImagecall that wasn't awaited. The Dart docs ofimageandwriteImagesay to await them when the order matters.Evidence
iOS 27.1 simulator, a test app running the old and the new handler code:
imagefollowed at once bywriteImage, 30 rounds: the earlier image every time, before and after.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
imageorwriteImagebefore a text clipboard call can see a different order. macOS still reads on the main thread and is unchanged here.