尝试再次查看pdf文件时,从错误线程访问的域Swift 3
我有收藏视图,用户可以点击下载pdf文件。如果下载完成,将显示pdf文件。但当pdf显示并返回后,再次点击单元格以再次显示pdf,它会抛出从错误线程访问的尝试再次查看pdf文件时,从错误线程访问的域Swift 3,swift,realm,alamofire,Swift,Realm,Alamofire,我有收藏视图,用户可以点击下载pdf文件。如果下载完成,将显示pdf文件。但当pdf显示并返回后,再次点击单元格以再次显示pdf,它会抛出从错误线程访问的域。 以下是我下载文件的代码: func downloadData(_ magazineObject: Magazine, cell: magazineCell) { Alamofire.download(magazineObject.urlMagazine, method: .get, encoding: JSONEncoding.defau
域。
以下是我下载文件的代码:
func downloadData(_ magazineObject: Magazine, cell: magazineCell) {
Alamofire.download(magazineObject.urlMagazine, method: .get, encoding: JSONEncoding.default, to: destination)
.downloadProgress(queue: DispatchQueue.global(qos: .utility)) { progress in
DispatchQueue.main.async() {
self.percentProgressFinal = Float(progress.fractionCompleted)
magazineObject.progressBarDownload = progress.fractionCompleted.roundToPlaces(places: 2)
magazineObject.progressBarTitle = Float(self.displayFinalLabelPercent).cleanValue
}
}
.validate { request, response, temporaryURL, destinationURL in
self.localPath = destinationURL
magazineObject.pathDatabase = String(describing: self.localPath!)
magazineObject.progressBarDownload = Double(self.percentProgressFinal)
magazineObject.progressBarTitle = Float(self.displayFinalLabelPercent).cleanValue
magazineObject.statusDarkViewAfterRelaunch = 1
let mainStoryBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc:PDFKBasicPDFViewer = mainStoryBoard.instantiateViewController(withIdentifier: "NEXT") as! PDFKBasicPDFViewer
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let filePath = "\(documentsPath)/\(magazineObject.title).pdf"
let document: PDFKDocument = PDFKDocument(contentsOfFile: filePath, password: nil)
vc.loadDocument(document)
self.navigationController?.pushViewController(vc, animated: true)
magazineObject.statusDownload = self.statusDatabase
let realm = try! Realm()
try! realm.write {
realm.add(magazineObject)
}
return .success
}
}
并显示文件(如果已下载):
if statusGlobal {
if magazineObject.statusDownload == 0 {
magazineObject.statusDownload = 1
//Alamofire Request
downloadData(magazineObject, cell: cell)
cell.progressDownload.isHidden = false
cell.progressLabel.isHidden = false
} else {
let mainStoryBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc:PDFKBasicPDFViewer = mainStoryBoard.instantiateViewController(withIdentifier: "NEXT") as! PDFKBasicPDFViewer
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let filePath = "\(documentsPath)/\(magazineObject.title).pdf"
let document: PDFKDocument = PDFKDocument(contentsOfFile: filePath, password: nil)
vc.loadDocument(document)
self.navigationController?.pushViewController(vc, animated: true)
}
}
}
< > P>但如果我重新运行模拟器,它可以显示我的PDF文件。< P>用来在抛出C++异常时设置断点,然后查找抛出“不正确的线程”异常的堆栈跟踪,以查看代码的哪一行触发异常。从这里,您应该能够确定跨线程错误共享的领域对象。我通过创建另一个变量来解决这个问题
let newMagazineObject = Magazine()
newMagazineObject.id = magazineObject.id
newMagazineObject.pathDatabase = magazineObject.pathDatabase
newMagazineObject.progressBarDownload = magazineObject.progressBarDownload
newMagazineObject.progressBarTitle = magazineObject.progressBarTitle
newMagazineObject.statusDarkViewAfterRelaunch = magazineObject.statusDarkViewAfterRelaunch
newMagazineObject.statusDownload = magazineObject.statusDownload
let realm = try! Realm()
try! realm.write {
realm.add(newMagazineObject)
}
您在上面提供的代码是您唯一获得并使用
领域的地方吗?是的,这是我唯一获得并使用领域的地方