Search code examples
iosswiftalamofirexcode8

segmentation fault: 11, when using Alamofire.upload


I updated Alamofire to 4.0 for swift 3. (before i was using AFNetworking).

My Code:

func uploadImage(_ image: Data, withURLRequrest urlRequest: URLRequestConvertible, responseCallback: ((NetworkResponse) -> ())? = nil) {


    Alamofire.upload(multipartFormData: { multipartFormData in

        _ = multipartFormData.appendBodyPart(data: image, name: "imagefile", fileName: "image.jpg", mimeType: "image/jpeg")

        }, with: urlRequest) { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    switch response.result {
                    case .success(let json):

                        if let responseCallback = responseCallback {
                            responseCallback(.success(Response(response: json)))
                        }

                    case .failure(_): break

                    }

                }
            case .failure(let encodingError): break
            }

    }

}

I get a compile error:

Command failed due to signal: Segmentation fault: 11.

and is the details i get a lot of garbage paths and a stack trace (not from my code)

and in the end:

  1. While emitting SIL for 'uploadImage' at */Network/APIManager.swift:64:5

This in pointing to the function above.

Any suggestions? Any other REST request is working (not multipart).

Thanks


Solution

  • The API of multipartFormData has changed.

    The append method now look like this (Note that it doesn't return any value):

    func append(_ data: Data, withName name: String, fileName: String, mimeType: String)

    Here is the example from the Alamofire's README:

    Alamofire.upload(
        multipartFormData: { multipartFormData in
            multipartFormData.append(unicornImageURL, withName: "unicorn")
            multipartFormData.append(rainbowImageURL, withName: "rainbow")
        },
        to: "https://httpbin.org/post",
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint(response)
                }
            case .failure(let encodingError):
                print(encodingError)
            }
        }
    )