There is a chance that background uploads work with some adaption of the alamofire api and URLSession whit this code:
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(imageData, withName: "photo[image]", fileName: filename, mimeType: "image/jpg")
},
usingThreshold: UInt64(0), // force alamofire to always write to file no matter how small the payload is
to: "http://", // if we give it a real url sometimes alamofire will attempt the first upload. I don't want to let it get to our servers but it fails if I feed it ""
method: .post,
headers: headers,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let alamofireUploadTask, _, let url):
alamofireUploadTask.suspend()
defer { alamofireUploadTask.cancel() }
if let alamofireUploadFileUrl = url {
var request = URLRequest(url: URL(string: "https://yourserver.com/photoUploadEndpoint")!)
request.httpMethod = "POST"
for (key, value) in alamofireUploadTask.request!.allHTTPHeaderFields! { // transfer headers from the request made by alamofire
request.addValue(value, forHTTPHeaderField: key)
}
// we want to own the multipart file to avoid alamofire deleting it when we tell it to cancel its task
// so copy file on alamofireUploadFileUrl to a file you control
// dispatch the request to the background session
// don't forget to delete the file when you're done uploading
} else {
// alamofire failed to encode the request file for some reason
}
case .failure:
// alamofire failed to encode the request file for some reason
}
}
)
Unfortunately this wont work with Alamofire 5, is there a way to adapt this?
Alamofire 5 doesn't support background
URLSessionConfigurations, but the code you posted is just a multipart upload, which is still supported just fine.Your various hackery around the upload should be reevaluated for the new version of Alamofire. It really doesn't make any sense.
Alamofire does work with background tasks, which can give you time to finish uploads. You can read more here.