App crashes when uploading large(?) video file (BC)
I use the 'Swift HTTP backwards compatibility branch' for my project and my app crashes when I try to upload a video (~3 minutes, iPhone 6 camera quality).
My code:
let fileDescription = mediaType.descriptionForApi
let fileName = "file" + mediaType.extensionForApi
let mimeType = mediaType.mimeTypeForApi
let task = HTTPTask()
task.requestSerializer = HTTPRequestSerializer()
task.requestSerializer.headers["x-key"] = XKEY
task.requestSerializer.headers["Authorization"] = "Bearer \(User.sharedInstance.access_token!)"
var sessionTask : NSURLSessionTask? = NSURLSessionTask()
do {
let data = try NSData(contentsOfURL: fileUrl, options: NSDataReadingOptions.DataReadingMappedIfSafe);
sessionTask = task.upload(self.createUrl(url), method: method, parameters: [fileDescription: HTTPUpload(data: data, fileName: fileName, mimeType: mimeType)], progress: { (value: Double) in
progress(value)
}, completionHandler: { (response: HTTPResponse) in
if let error = response.error {
// Failed
let json = JSON("")
dispatch_async(dispatch_get_main_queue(),{
failure(error, json)
})
} else {
// Success
var json = JSON("")
if let data = response.responseObject as? NSData {
let str = NSString(data: data, encoding: NSUTF8StringEncoding)
json = JSON(str!)
}
dispatch_async(dispatch_get_main_queue(),{
success(json)
})
}
})
} catch let error {
print("NSURL naar NSDATA error: \(error)")
}
Sometimes I get this error:
Mevolution(1086,0x1a1bf9000) malloc: *** mach_vm_map(size=750813184) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Mevolution(1086,0x1a1bf9000) malloc: *** mach_vm_map(size=750813184) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Other times I get (SwiftHTTP: HTTPRequestSerializer.swift):

My Phone is running iOS 9.0 and my app uses Swift 2.
Does anyone have any idea what could be the problem?
Edit: I think it's because my video file eats all the memory. How can I upload large files?
Yeah from the malloc error, I bet it is because the video is too large and the device runs out of memory. We would need to be able to stream the upload to keep this from happening. It isn't really setup right now to do something like that, so it will probably take some thought and exploration of the APIs to figure out the best solution.
https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSession_class/index.html#//apple_ref/occ/instm/NSURLSession/uploadTaskWithRequest:fromFile:
That API is what I would reach for first, but if memory serves it is fairly limited in what it can do with multipart wise. Might be improved in iOS 9 or a possible solution built off of it though.