gpt4 book ai didi

ios - CollectionView-将pdf文件拖放到collectionView上(loadObject(ofClass :)无法正常工作)

转载 作者:行者123 更新时间:2023-12-01 16:22:12 25 4
gpt4 key购买 nike

我在通过拖放 session (使用 UICollectionView 's Drag&Drop feature)加载pdf文件时遇到问题。

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文件,则在保存上下文时会导致合并错误(因为每个删除的文件同时运行后台任务) 。

有什么想法吗?是否不允许从另一个线程中加载对象?

最佳答案

自看代码以来已经有很长时间了,但是我希望这对您有所帮助:

对我来说,解决方案是使用loadObjects(ofClass:completion:)方法。这将异步获取所需文件的所有对象。 documentation中未说明。但是,如果您按cmd单击该方法并转到其声明,则会出现以下注释:

/* A convenience method that can be used only during the UIDropInteractionDelegate's
* implementation of `-dropInteraction:performDrop:`.
* Asynchronously instantiates objects of the provided class for each
* drag item that can do so. The completion handler is called on the
* main queue, with an array of all objects that were created, in the
* same order as `items`.
* The progress returned is an aggregate of the progress for all objects
* that are loaded.
*/
func loadObjects(ofClass aClass: NSItemProviderReading.Type, completion: @escaping ([NSItemProviderReading]) -> Void) -> Progress

实例化对象后,在主队列上调用完成处理程序。从这里,您可以调度到后台队列,并对文件进行任何操作。

我获取了最终得到的代码并对其进行了简化:

// ValidComicBookFile is my NSItemProviderReading type that accepts multiple file types, like archive and pdf
coordinator.session.loadObjects(ofClass: ValidComicBookFile.self) { (importedObjects) in
guard let validComicBookFiles = importedObjects as? [ValidComicBookFile] else {
// Handle errors
}

DispatchQueue.global(qos: .userInitiated).async {
// ...
for (index, file) in validComicBookFiles.enumerated() {
switch file.fileType {
case .archive:
// Unpack it and stuff
case .pdf:
// For example:
if let pdfDoc = PDFDocument(data: file.data) {
pdfs.append(pdfDoc) // Append it to an array I created earlier
}
case .none:
break
}
}
// ...
}
}

一个注意事项:我最终并不需要保存到数据库,但是通过这种方式,(希望)这不是问题。

关于ios - CollectionView-将pdf文件拖放到collectionView上(loadObject(ofClass :)无法正常工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53563061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com