From 60e6db6efea5f6caa0fcdc1bfc9afe8a95363a96 Mon Sep 17 00:00:00 2001 From: Stanislau Bautovich Date: Mon, 16 Nov 2026 11:10:48 +0900 Subject: [PATCH] pasteboard: read and write the iOS image off the main thread 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. --- .../pasteboard/SwiftPasteboardPlugin.swift | 22 ++++++++++++------- packages/pasteboard/lib/pasteboard.dart | 8 +++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/pasteboard/ios/pasteboard/Sources/pasteboard/SwiftPasteboardPlugin.swift b/packages/pasteboard/ios/pasteboard/Sources/pasteboard/SwiftPasteboardPlugin.swift index d953e36e..80fbafec 100644 --- a/packages/pasteboard/ios/pasteboard/Sources/pasteboard/SwiftPasteboardPlugin.swift +++ b/packages/pasteboard/ios/pasteboard/Sources/pasteboard/SwiftPasteboardPlugin.swift @@ -3,6 +3,10 @@ import UIKit @objc(PasteboardPlugin) public class PasteboardPlugin: NSObject, FlutterPlugin { + // Pasteboard access can block, e.g. while another app still provides the + // data, so image and writeImage run off the main thread, in call order. + private static let queue = DispatchQueue(label: "one.mixin.pasteboard", qos: .userInitiated) + public static func register(with registrar: FlutterPluginRegistrar) { let channel = FlutterMethodChannel(name: "pasteboard", binaryMessenger: registrar.messenger()) let instance = PasteboardPlugin() @@ -32,10 +36,11 @@ public class PasteboardPlugin: NSObject, FlutterPlugin { } } - private func image(result: FlutterResult) { - let image = UIPasteboard.general.image - let data = image?.pngData() - result(data) + private func image(result: @escaping FlutterResult) { + Self.queue.async { + let data = UIPasteboard.general.image?.pngData() + DispatchQueue.main.async { result(data) } + } } private func files(result: FlutterResult) { @@ -46,10 +51,11 @@ public class PasteboardPlugin: NSObject, FlutterPlugin { result(nil) } - private func writeImageToPasteboard(_ data: Data, result: FlutterResult) { - let image = UIImage(data: data) - UIPasteboard.general.image = image - result(nil) + private func writeImageToPasteboard(_ data: Data, result: @escaping FlutterResult) { + Self.queue.async { + UIPasteboard.general.image = UIImage(data: data) + DispatchQueue.main.async { result(nil) } + } } } diff --git a/packages/pasteboard/lib/pasteboard.dart b/packages/pasteboard/lib/pasteboard.dart index 158504eb..97c037d5 100644 --- a/packages/pasteboard/lib/pasteboard.dart +++ b/packages/pasteboard/lib/pasteboard.dart @@ -8,6 +8,10 @@ class Pasteboard { /// Returns the image data of the pasteboard. /// /// available on iOS, Desktop, Web, Android + /// + /// iOS: runs off the main thread, in order with [writeImage]. Await it + /// before calling [text], [writeText] or Flutter's `Clipboard` if the + /// order matters. static Future get image => pasteboard.image; /// only available on Windows and the web. @@ -18,6 +22,10 @@ class Pasteboard { /// available on iOS, Android, Web /// /// set image data to system pasteboard. + /// + /// iOS: runs off the main thread, in order with [image]. Await it before + /// calling [text], [writeText] or Flutter's `Clipboard` if the order + /// matters. static Future writeImage(Uint8List? image) => pasteboard.writeImage(image);