I'm trying to find cancel request method in Moya but seem it's not support. Let me know if i missing something from document.
Yes, that's possible! Have a look at the providers docs:
That's it! The
request()method returns aCancellable, which has only one public function,cancel(), which you can use to cancel the request.
@BasThomas thanks, i got it
In my case, i call request_1, but this not response, i continue call request_2, so i want canceled request_1 (mean only 1 request available at the same time). Do we have any property or method to do that?
@bestwnh i think this way not make sense in search function. Before i call request_2, i check is there any request at this time, if have, i'll canceled all.
@nguyentrai2011 You could store the request_1 like
let request_1 = MoyaRequestXXXXX
then before you call request_2 call, just call request_1.cancel().
You also can add the request into an array. Then before you make new request, just use requestArray.forEach({ $0.cancel() }).
@bestwnh i got it, thanks. I ask to make sure did Moya support or not this feature because i see we have Manager(type: Alamofire.SessionManager) in MoyaProvider.
@BasThomas Is the SessionManager can make a better solution for this situation?
Unfortunately, I don't know.
If you need it rather universal, meaning that at any moment in the app, only 1 request can be fired up at once, you would need to implement your own logic for that, using e.g. OperationQueue.
However, if you want it for only one, special case, you could make use of requestClosure. You could fire up request_1 in that closure with a check that you are firing request_2. Now based on the response from request_1, you can decide whether request_2 should be performed or not using this closure. Unfortunately this way you probably can't propagate the response from request_1 easily and thus this is a workaround.
i think the way of @bestwnh is good & easy for me now thanks all
Most helpful comment
Yes, that's possible! Have a look at the providers docs: