Question

You enter the URL in the address string of your browser. It takes long time to appear in the client area. You are bored and type another address instead and click enter. Which page will be rendered in the browser, first or the second?

A similar issue raises when you update the outline of your document while typing you the editor. In Eclipse you have a separate Outline window, which should be in sync with the document. It is updated whenever you type. You edited your document, the update launches re-parse or build. Meantime, you go on typing, starting another parse. When parser finishes, it updates the Outline window. What if first parse finishes after the first one? The outdated Outline prevails!

How do you solve this problem in the async programming? Should you check if your action is obsolete in the continuation or cancel all current action before launching new ones?

Was it helpful?

Solution

Depending on how expensive the action is on resources you have the choice between explicit cancel or check after the fact.

The check after completion is the simplest to implement, in the completion handler check if it's still valid.

Doing the cancel is a bit trickier as you need to keep a handle to the task and send it a cancel signal. Then in the task itself you must check whether it was canceled regularly and then abort and clean up as needed.

Licensed under: CC-BY-SA with attribution
scroll top