daltoniam / SwiftHTTP

Thin wrapper around NSURLSession in swift. Simplifies HTTP requests.
Apache License 2.0
1.88k stars 313 forks source link

App crashes when uploading large(?) video file (BC) #158

Open NSURLSession0 opened 9 years ago

NSURLSession0 commented 9 years ago

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):

schermafbeelding 2015-09-17 om 21 21 28 schermafbeelding 2015-09-17 om 21 21 42

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?

daltoniam commented 9 years ago

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.