I'm developing an upload project in swift. I'm taking very large files (video, picture with size over 500 MB) with imagepickercontroller and dividing this file into chunks which has a size 1 MB. Then I send these chunks to remote server and make them defragment in server and I'm showing this file to user.
I have no problem if the file size is under 300 MB. But after this size, memory goes up too much and app is being crashed. Actually, in every case memory usage are raising but there is no crash.
When I watch progress on console, I see URLSession task begins. But, because of these tasks are waiting response from completion handler, the task queue is growing and memory usage goes up. Is there a way when a task begins, this task's completion handler begins too? I think if I can make task queue free concurrently, my problem solves. I'm waiting your helps.
let url:URL = URL(string: "\(addressPrefix)UploadFile")!
let session = URLSession.shared
let request = NSMutableURLRequest(url: url)
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
request.httpMethod = "POST"
let bodyData = "\(metaDataID)~\(chunkIndex)~\(chunkSize)~\(chunkHash)~\(wholeTicket)~\(fileDataString)"
request.httpBody = bodyData.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue));
request.timeoutInterval = .infinity
let task = session.dataTask(with: request as URLRequest, completionHandler: {(data, response, error) in
guard let _:Data = data, let _:URLResponse = response, error == nil else {
var attemptCounter = 1
if attemptCounter <= 3 {
completion("\(attemptCounter).attempt",chunkSize, error)
attemptCounter += 1
}
return
}
let jsonStr = String(data: data!, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
completion(jsonStr, chunkSize, error)
SingletonConnectionManager.sharedConnectionDataManager.dataTasks["uploadFile"] = nil
})
SingletonConnectionManager.sharedConnectionDataManager.dataTasks["uploadFile"] = task
task.resume()
---I call this URLSession task from this function in a tableview controller
tmpConnection.uploadFile(chunk, metaDataID!, chunkIndex: chunkIndex, completion: {(result, chunkSize, error) in
// I want to enter immediately when 'uploadFile' get called })