I am using QlPreviewController in my applications, I am download the data one by one using observer, The observer detects currentIndexItem in the preview controller, The image is only download when the currentIndexItem will changed ,After downloading i will refresh the currentPreviewItem , My Problem is , while downloading an files in first item and i am moving to the next items , which also downloading an files,And both screens are freezing , after click the menu button, in the sections show images is loaded , but not reflected.
While i am calling download data, The thread is changed to Background thread
DispatchQueue.global().async {
interactor.processData(kbPath: categoriesPath)
}
}else{
interactor.processData(kbPath: categoriesPath)
}
This is the downloading file which will happens in Background thread, The above code will change
let url = fileData.contentUrl
let fileName = fileData.name
let contentUrl = fileData.contentUrl
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let destinationURL = documentsURL.appendingPathComponent(fileName)
// Check if the file already exists at the destination URL
if FileManager.default.fileExists(atPath: destinationURL.path){
return completion(.success(File(data: destinationURL, name: fileName, contentUrl: contentUrl, hasData: true)))
}
// If the file doesn't exist, proceed with downloading
let request = URLRequest(url: URL(string: url)!)
let task = URLSession.shared.downloadTask(with: request) {(tempFileURL, response, error) in
if let error = error {
print("Error downloading file: \(error)")
completion(.failure(.FailedToDownload))
return
}
guard let httpResponse = response as? HTTPURLResponse , httpResponse.statusCode == 200,let tempFileURL = tempFileURL else {
print("Failed to download file. Invalid response.")
completion(.failure(.FailedToDownload))
return
}
do {
try FileManager.default.moveItem(at: tempFileURL, to: destinationURL)
completion(.success(File(data: destinationURL, name: fileName, contentUrl: url, hasData: true)))
} catch {
print("Error moving file: \(error)")
completion(.failure(.FailedToDownload))
}
}
task.resume()
}
After Downloading the data , it will inform to the viewcontroller to check the thread and call
DispatchQueue.main.async {
handler()
}
}else{
handler()
}
This is my Observer
if context == &previewControllerContext{
if keyPath == "currentPreviewItemIndex" {
let currentIndex = preview.currentPreviewItemIndex
//api calls for the specific files
if currentIndex < previewData.count && !previewData[currentIndex].hasData {
self.loadingView.startAnimating()
let builder = Builder()
self.previewData[currentIndex].hasData = true
builder.build(instance: self, categoriesPath: .KBDownloadFile(fileData: self.previewData[currentIndex]))
}
}
}else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
After downloaded image the result will notify and i will refresh the current previewItem self.preview.refreshCurrentPreviewItem()
The Issue video link -> https://drive.google.com/file/d/18pBZawbvcUU3SA5vS53X9R9q-AGMG5Sv/view
Any one facing this issues with this ,my thought this issues is thread based , but i will handle the thread