Skip to content

Cancel outstanding operations#66

Merged
krbarnes merged 2 commits intomasterfrom
cancel-old-operations
Jan 18, 2018
Merged

Cancel outstanding operations#66
krbarnes merged 2 commits intomasterfrom
cancel-old-operations

Conversation

@krbarnes
Copy link
Copy Markdown
Contributor

In some situations operations can pile up in the renderAndDiff queue. It's not necessary to run all of them because the last one added should provide the correct state to display. This PR cancels any outstanding operations that are not already executing before adding the new operation to the queue.


strongSelf.doRenderAndDiff(newSections, animated: animated, animations: animations, completion: completion)
}
renderAndDiffQueue.operations.forEach { (operation) in
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

renderAndDiffQueue.operations.lazy.filter { !$0.isExecuting }.forEach { $0.cancel() }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Won’t this iterate over the array twice instead of once? Why lazy?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lazy makes it iterate over elements only once :)

["A", "B", "CD", "EF"].lazy.filter { 
    print("filter") 
    return $0.count > 1 
}.forEach { 
    print("forEach") 
    $0.lowercased() 
}

spits out:

filter
filter
filter
forEach
filter
forEach

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(but I don't actually care, lazy is pretty cool though and makes for nice functional code without losing efficiency)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

TIL.gif

@etiennemartin
Copy link
Copy Markdown

What should we do about the callbacks passed in? There was a discussion about this before and I'm not sure it was resolved. Would there be any value in calling the completion block with some type of status enum (done, cancelled, failed?). The goal would be to provide more context to the callback to react to the completion accordingly?

Just a thought.

@krbarnes
Copy link
Copy Markdown
Contributor Author

krbarnes commented Jan 18, 2018

The completion callbacks are still called on cancelled operations.
I don't think there's value in creating a new callback with additional state. The operation was cancelled because you've passed new state to the table that preempts the old one.

@krbarnes krbarnes merged commit 8decf03 into master Jan 18, 2018
@krbarnes krbarnes deleted the cancel-old-operations branch January 18, 2018 17:58
@raulriera
Copy link
Copy Markdown
Contributor

You should make a release 😜 (update the CHANGELOG, tag the framework)

@krbarnes
Copy link
Copy Markdown
Contributor Author

200

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.

6 participants