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

Swift Concurrency - Docs Unclear - Upload Request Progress #3552

Open
thatMacAdmin opened this issue Jan 31, 2022 · 3 comments
Open

Swift Concurrency - Docs Unclear - Upload Request Progress #3552

thatMacAdmin opened this issue Jan 31, 2022 · 3 comments

Comments

@thatMacAdmin
Copy link

Problem

The current docs around swift concurrency are unclear. How do you get the progress from AF.upload(file, to: url)

Feature Request

Please update the docs with an upload example.

Value to Alamofire

This will make the new concurrency more accessible to new developers like myself!

@jshier
Copy link
Contributor

jshier commented Jan 31, 2022

UploadRequest has the same progress as DataRequest, as it's a subclass. I don't tend to give UploadRequest specific examples because it has exactly the same interface as DataRequest aside from some upload specific functionality. I guess I can make most references to DataRequest read DataRequest and UploadRequest though.

@thatMacAdmin
Copy link
Author

Looking at the docs I coded this:

func upload(file: URL) async throws -> String {
        let putURL = try await self.getPutURL(key: file.lastPathComponent, expiration: 30)
        
        let request = AF.upload(file,to: putURL, method: .put)
        
        for await progress in request.uploadProgress() {
            debugPrint("Progress: \(progress.fractionCompleted * 100)")
        }
        
        guard let etag = await request.validate()
                .serializingData().response.response?.headers["Etag"] else {
                    throw S3Error.unableToDecodeResponse
                }
        
        return etag
    }

But this does not work. It works without the progress block.. but not with. I would post on StackOverflow but this is still pretty new....

@jshier
Copy link
Contributor

jshier commented Feb 1, 2022

It's working correctly, which is why many of my progress examples include throwing the uploadProgress() into its own Task. for await suspends awaiting new values and only completes the loop when progress is complete. Unfortunately, since you suspend before actually starting the request, no progress values are ever received and the code is suspended forever. For progress I recommend still using the older closure APIs.

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

No branches or pull requests

2 participants