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

upload request #11

Open
kolisko opened this issue Oct 25, 2016 · 7 comments
Open

upload request #11

kolisko opened this issue Oct 25, 2016 · 7 comments

Comments

@kolisko
Copy link

kolisko commented Oct 25, 2016

Hi,

I would like to ask you if is possible to add upload request synchronous please?

Alamofire.upload(data, to: url)

Thank you,
Michal

@Dalodd
Copy link
Owner

Dalodd commented Oct 26, 2016

Already supports that if you use Alamofire 4.0/AlamofireSynchronous 4.0

let response = Alamofire.upload(data, to: url).responseJSON();

@philippeauriach
Copy link

philippeauriach commented Dec 8, 2016

Maybe @kolisko was asking about the multipart form data ? I did not find a better way than to add a new function using the same method you did :

class AlamofireSynchronous {
    class func uploadRequest(multipartFormData: @escaping (MultipartFormData) -> Void, to: URLConvertible, method: HTTPMethod, headers: HTTPHeaders? = nil) -> UploadRequest? {

        let semaphore = DispatchSemaphore(value: 0)
        var result: UploadRequest? = nil

        Alamofire.upload(multipartFormData: multipartFormData, to: to, method: method, headers: headers) { (res: Alamofire.SessionManager.MultipartFormDataEncodingResult) in

            switch res {
            case .success(let upload, _, _):
                result = upload
            case .failure(let e):
                log.error("Error while creating upload request : \(e)")
            }

            semaphore.signal()
        }
        
        _ = semaphore.wait(timeout: DispatchTime.distantFuture)
        return result
    }
}

this allows to call the following :

if let req = AlamofireSynchronous.uploadRequest(multipartFormData: { (multipartFormData: MultipartFormData) in
        //TODO: append your form data
    }, to: "http://www.upload.com", method: .post) {
        let res = req.responseJSON()
        //TODO: use your res
}

If this is not what @kolisko asked for, I'm ok with bringing this to a new issue. I'd like to make a PR about this, but do you see a better way to make this fit the actual project, instead of creating a new class ?

@cheizer
Copy link

cheizer commented Jan 10, 2017

I would love to see multipartFormData added without adding the separate class.

@philippeauriach
Copy link

@cheizer if you have any idea how to, I'm open to suggestions :)

@cheizer
Copy link

cheizer commented Jan 15, 2017

I'm looking in to it, I have a temporary solution based on the code you posted for @kolisko. The problem I had with it was the semaphore was not working 100% of the time at least not in the debugger using Xcode 8.2.1. It's like the semaphore.wait was being ignored so the result was always nil.

@aravasio
Copy link

aravasio commented Apr 9, 2017

Ok, I know this is late, but @cheizer, you also had that semaphore issue? I'm experiencing that on Xc 8.3 and I thought I was doing something inexplicably dumb.

Any updates?

@cheizer
Copy link

cheizer commented Nov 25, 2017

No, I've moved on, I still have the issue.

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

5 participants