【发布时间】:2018-11-30 18:36:10
【问题描述】:
我从放置会话加载 pdf 文件时遇到问题(使用 UICollectionView's Drag&Drop feature)。
在collectionView(_:performDropWith:coordinator) 内部,我想在后台线程中加载丢弃的项目:
func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
// I'm dropping a valid pdf file from the Files app in iOS.
// I'm using performBackgroundTask because I want to save stuff to the database
appDelegate.persistentContainer.performBackgroundTask({ (privateContext) in
for item in coordinator.items {
// This correctly returns true
if item.dragItem.itemProvider.canLoadObject(ofClass: MyPDFDocument.self) {
item.dragItem.itemProvider.loadObject(ofClass: MyPDFDocument.self) { (pdfItem, error) in
// This is not called
}
}
}
})
}
final class MyPDFDocument: PDFDocument, NSItemProviderReading {
public static var readableTypeIdentifiersForItemProvider: [String] {
return [kUTTypePDF as String]
}
public static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> ComicBookPDFDocument {
return MyPDFDocument(data: data)!
}
}
但是,它不起作用。块loadObject(ofClass:) 应该被调用,根本没有被调用。它在主线程上运行良好。
问题是,我不能将 performBackgroundTask 块放在 loadObject(ofClass:) 中(然后删除的对象可以完美加载),因为如果你删除多个 pdf 文件,这会在保存上下文时导致合并错误(因为后台任务为每个删除的文件同时运行)。
有什么想法吗?不允许从另一个线程中加载对象吗?
【问题讨论】:
-
我知道这是旧的......但你最终做了什么?我也有同样的问题,很奇怪。
-
所以,我已经很久没有遇到这个问题了。我想我确实找到了方法。我要发布一个答案。也许它有帮助:D
标签: ios swift uicollectionview drag-and-drop