I am implementing dragging and dropping images from, let say, Safari into a collectionView of my own app. I need the both the UIImage and image url from the dragItem.
The way to get any of them is to use UIDragItem.itemProvider.loadObject
. But since loadObject(ofClass:completionHandler:)
runs asynchronously, how can I make sure there will be no race condition with the code below?
(note: the two are in the performDropWith
func, I want to make sure they execute in order because I need to do work immediately after the second one finishes)
var imageARAndURL = [String: Any]()
_ = item.dragItem.itemProvider.loadObject(ofClass: UIImage.self) { (provider, error) in
if let image = provider as? UIImage {
let aspectRatio = image.size.height / image.size.width
print(aspectRatio)
imageARAndURL["aspectRatio"] = aspectRatio
}
}
_ = item.dragItem.itemProvider.loadObject(ofClass: URL.self) { (provider, error) in
if let url = provider {
print(url.imageURL)
imageARAndURL["imageURL"] = url.imageURL
print(imageARAndURL)
}
}