Thursday, January 4, 2024
HomeiOS Developmentios - Downloading an enormous file with URLSession downloadTask crashes at finish

ios – Downloading an enormous file with URLSession downloadTask crashes at finish


I must obtain an enormous mp4 file (>2gb) and it crashes on an iPhone mini however not on a brand new iPhone 15.

The entire obtain works properly and the reminiscence is secure however when it finishes the reminiscence spikes till the app crashes.
I used to do strive FileManager.default.moveItem(at: location, to: destinationURL) since I wish to put it aside in a particular listing however even commenting the road the app runs out of reminiscence. Any concept do it?

This and print("File downloaded efficiently!") the profitable completionHandler get known as.

personal func downloadFile(from url: URL, identify: String, progressHandler: @escaping (Double) -> Void, completionHandler: @escaping (URL?, Error?) -> Void) {
        let activity = URLSession.shared.downloadTask(with: url) { location, response, error in
            if let error = error {
                print("Error downloading file:", error)
                completionHandler(nil, error)
                return
            }
            
            guard let location = location else {
                print("Obtain location not accessible.")
                completionHandler(nil, nil)
                return
            }
            
            guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
                print("Paperwork listing not discovered.")
                return
            }

            let destinationURL = documentsDirectory.appendingPathComponent("(identify).mp4")
            do {
//                strive FileManager.default.moveItem(at: location, to: destinationURL)
                print("File downloaded efficiently!")
                completionHandler(location, nil)
            } catch {
                print("Error shifting downloaded file:", error)
                completionHandler(nil, error)
            }
        }
        
        let downloadProgress = activity.progress
        let progressQueue = DispatchQueue(label: "com.instance.progressQueue", qos: .utility, attributes: .concurrent)
        
        let progress = Progress(totalUnitCount: 100)
        progress.completedUnitCount = 0
        
        let progressTimer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { timer in
            progressQueue.async {
                progress.completedUnitCount = Int64(downloadProgress.fractionCompleted * 100)
                progressHandler(progress.fractionCompleted)
            }
            
            if progress.isFinished {
                timer.invalidate()
            }
        }
        
        activity.resume()
    }



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments