Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
NSURLSession0 opened this issue Sep 17, 2015 · 1 comment
Open

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

NSURLSession0 opened this issue Sep 17, 2015 · 1 comment

Comments

@NSURLSession0
Copy link

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants