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

fix(AsyncOperation): cancel() and isCancelled behavior #801

Merged
merged 9 commits into from Aug 15, 2023

Conversation

KaiOelfke
Copy link
Contributor

Summary

The operations returned by the SDK didn't behave as I expected regarding cancellation. I am by no means an expert on the intricacies of (NS)Operation.

AsyncOperation doesn't override isCancelled and doesn't call super.cancel() so isCancelled never becomes true as far as I understand. But there's checks for isCancelled in HTTPRequest and AsyncOperation.start().

I added some unit tests with AsyncOperation and a BlockOperation to see the standard behavior.

To pass the tests I came up with two possible solutions. Either remove the cancel override so that the super implementation of Operation is used or override and manage isCancelled.

Result

With these changes cancellation works as I'd expect. The behavior differs also as the state is not immediately set to finished anymore. Either the operation didn't yet start and will be set to finished once start() is called or the operation is already executing. For the second case it's up to the subclass to optionally handle cancellation and to set the state to finished.

I also added a line in the HTTPRequest isCancelled guard to set the result variable to a cancellation error. So that the completion block gets also called once for an operation that's cancelled early before the URL task is created.

KaiOelfke and others added 5 commits October 4, 2022 13:22
Without setting the result the completion handler will never be called, but users of the API should expect a callback with a cancellation error as failure result.
Copy link
Contributor

@VladislavFitz VladislavFitz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @KaiOelfke ,
Thank you for your contribution and sorry for delay.
Just remove the commented code and it's all good 👍

Sources/AlgoliaSearchClient/Async/AsyncOperation.swift Outdated Show resolved Hide resolved
# Conflicts:
#	.github/workflows/carthage.yml
#	.github/workflows/pods.yml
@VladislavFitz VladislavFitz changed the title AsyncOperation cancel() and isCancelled behavior fix(AsyncOperation): cancel() and isCancelled behavior Aug 15, 2023
@VladislavFitz VladislavFitz merged commit c060289 into algolia:master Aug 15, 2023
2 of 3 checks passed
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

Successfully merging this pull request may close these issues.

None yet

2 participants