In a Mac OS X app (Cocoa), I'm copying some images from my app to others using a NSDraggingSession
. The NSDraggingItem
makes use of an object that implements the protocol NSPasteboardItemDataProvider
, to provide the data when the user drops it.
As I'm dealing with images, the types involved are: NSPasteboardTypePNG
, kPasteboardTypeFileURLPromise
, kUTTypeFileURL
, com.adobe.photoshop-image
and public.svg-image
. These images are in a remote location, so before I can provide them to the pasteboard, I have to download them from the Internet.
I implement the method - pasteboard(pasteboard:item:provideDataForType:)
doing something like this:
If the type requested is
kPasteboardTypeFileURLPromise
, I get the paste location and build and set in the pasteboard the URL string with the location where the file is supposed to be written in the future.If the type requested is
kUTTypeFileURL
, I download the file, specify a temporal location and write the downloaded file to that location. Then, I set in the pasteboard the URL string of the location.If the type requested is one of the others, I download the file and set the plain
NSData
in the pasteboard.
All these operations are performed on the main thread, producing some lags that I want to get rid of.
I've tried to perform these operations on a background thread, and come back to the main thread to set the final data in the pasteboard, but this doesn't work because the method finishes before.
Does anyone know a way to achieve it?