If you can keep your HEAD when all about you
Are losing theirs and blaming it on you,
If you can trust yourself when all men doubt you,
But make allowance for their doubting too;
If you can wait and not be tired by waiting,
Or, being locked away, don’t deal in locks,
Or, being raced, don’t give way to racing,
And yet don’t loop too good, nor thread too wise:
Downloading images asynchronously is hard due to complexities involving concurrency, speed, and user experience. In this post, I will implement a simple yet effective image downloader with memory and disk caches.
Update: The solution in this post is effectively obsoleted by the
guard statement available since Swift 2.
Swift 1.2 introduces a feature that allows us to condense nested
if statements into a single one, eliminating the so-called “pyramid of doom”. However, there are still a few edge cases not covered by such an improvement, one of which appears in the Tiny Networking library by Chris Eidhof:
What prevents us from using a single
if statement is the fact that there is a corresponding
else after each
if. In fact, this is a very common pattern in data serialisation and error handling, so we need a more elegant way to represent it.