[swift5] Cancellable requests#10855
Conversation
There was a problem hiding this comment.
@4brunu I have one question about this.
Don't we want to wrap this Future publisher into a Deferred publisher?
So it would work like Rx's Single by having it defer its execution until it receives a subscriber.
There was a problem hiding this comment.
I don't have much experience with RxSwift or Combine.
Is the current RxSwift integration lazy (defer its execution until it receives a subscriber)?
There was a problem hiding this comment.
@4brunu Yes. RxSwift Observable's only call when you subscribe to the stream.
In Combine publishers are working the same way except Future. We can only achieve the same if we would wrap Future in Deferred.
So currently if we just write this:
let newPet = Pet(id:<id>, category: <category>, name: <name>, photoUrls: [])
let addPetPublisher = PetAPI.addPet(body: newPet)The request will be called without subscribing to the stream with .sink
There was a problem hiding this comment.
This is the way it is, because when I introduced support for Combine I didn't have much experience with it, and Future looked like a good fit.
I think we can also make Combine lazy, because that would make the API more flexible, by only starting the request when the user want's.
What do you think?
There was a problem hiding this comment.
Yes, I think we should do that 👍
There was a problem hiding this comment.
Can this behaviour change create any bugs for the users that already implement Combine in their projects?
For example a network request that never happens because of this change?
There was a problem hiding this comment.
Unfortunately, yes 😕
There was a problem hiding this comment.
If somebody used it without subscribing to the stream, it won't be called
There was a problem hiding this comment.
Hum, let's keep that for a different PR then, and keep this one with just the cancellation, to avoid any friction to merge it.
There was a problem hiding this comment.
Oh yeah for sure
a28ddcc to
eb542d6
Compare
|
Hey @davidhorvath, this is another great PR, thanks 👍 Instead of returning an We should also support cancellation in Alamofire, by replace the following code In the following class Those two changes should a lot of conditions in the templates. We should also support request cancelation in |
|
Looks like the way to cancel an async/await function is https://github.com/apple/swift-evolution/blob/main/proposals/0304-structured-concurrency.md#cancellation-handlers Here is the example in the like above. |
@4brunu Unfortunately this is not working the way it suppose to 😕 |
|
@4brunu
Still missing:
|
a1e1a11 to
1cbbc29
Compare
1cbbc29 to
d221576
Compare
|
@davidhorvath I think I got Just for reference here are the links where I found the solution. |
d221576 to
98c4a77
Compare
98c4a77 to
a1e9943
Compare
a1e9943 to
d4a37a5
Compare
|
@davidhorvath I don't want to be annoying, but I got one last improvement that it would be nice, but if you prefer I can do it in a separated PR. What do you think? |
@4brunu Sure I can add that as well |
d4a37a5 to
f13e5aa
Compare
4brunu
left a comment
There was a problem hiding this comment.
Looks good to me 👍
@davidhorvath Thank you very much for the two awesome PRs, that were very impactful in the Swift Generator, and for your quality PRs.
Thanks you also for your patience and for doing the required changes to merge the PRs.
Are you planning to do open more PRs? 😃
Feel free to contribute any time you want 👍
resolve #8658
Previously the generated clients didn't support cancellation of requests
I've added request cancellation for:
For these clients I have updated the RequestBuilder's execute function so it returns an optional
URLSessionDataTask.I've also added
@discardableResultto keep backward compatibility.Default
I've added to return the
URLSessionDataTaskso the user will be able to control the request's lifecycle.Result
I've added to return the
URLSessionDataTaskso the user will be able to control the request's lifecycle.Combine
I've implemented handeEvents(receiveCompletion: where I cancel the returned
URLSessionDataTaskwhen the stream is cancelledRxSwift
I've added to cancel the returned
URLSessionDataTaskinDisposables.createwhen the stream is cancelledAsync Await
I've added to cancel the returned
URLSessionDataTaskinwithTaskCancellationHandler.onCancel:PR checklist
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.For Windows users, please run the script in Git BASH.
master(5.3.0),6.0.x