Once-a-year Black Friday deals are coming. Read more.
Give your simulator superpowers

RocketSim: An Essential Developer Tool
as recommended by Apple

Issue 58
Apr 13, 2021

I tried something new.

This week, I decided to rethink my newsletter intro. It's often hard for me to develop a valuable topic, and I want to make this newsletter as valuable as possible.

Therefore, I decided to handle one of the questions asked weekly. I reached out on Twitter and got several great questions from which I'm handling one today:

How many eggs do your chickens lay?

Alright, of course, I'm not picking this one! (The answer is 3 btw). Instead:

What’s your opinion on the best practice to retry a failing network request?

This question by Vincent Pradeilles received a lot of likes which proves it's an interesting topic! And of course, it is: almost every app performs requests, and what do we do if that network request fails?

First of all; There are a few points I want to clear out:

----

You don't want to retry each failing network request
Quoted from WWDC Session Advances in Networking:
"Avoid retrying URLSessionTasks due to lack of network connectivity."

Only retry up to 3 times max.
It's not worth retrying more than 3 times. There's probably something wrong, which is why the request failed.

Try to handle the error instead.
What is the reason your request fails? It might be an authentication error or something else. It's likely better to handle this error if chances are high you'll receive the same response again.

----

So, if you really want to do retrying, my personal recommendation would be to use Alamofire's built-in solution. I know many of you prefer to use URLSession, but in my opinion, retrying is one of those reasons to adopt a popular framework like Alamofire. It's well-documented and explains how you can implement a retrying mechanism taking the above points into account. Sure, you shouldn't easily add a 3rd party dependency, but if it saves you from writing a lot of custom logic around networking, it might be worth it!

Hopefully, that was a useful intro! I feel like some of the questions are too big for a blog post but big enough to handle here. Next week on Monday, I'll reach out to you on Twitter again for some questions!

Enjoy this week's SwiftLee Weekly!

THIS WEEK'S BLOG POST

Adopting SwiftUI in an early state often requires us to apply view modifiers conditionally. A common use case for me is to apply bug fixes for iOS 13 only. Doing this isn't easy with default APIs, which is why I created a few useful extensions to make this easier.

TWEET OF THE WEEK

I loved this idea by Peter Steinberger to debug the SwiftUI loop of updating views. It’s good to look at the thread, and useful links are shared to, for example, how Xcode Instruments can help as well.

ROCKETSIM 4.0 RETWEET CONTEST

It's already a month ago since I released RocketSim 4.0 and last weekend the 4.1.0 release arrived. During the initial launch there has been a contest to win a Pro license for which I've picked three winners:
- Pawan Sharma
- Shawn

Thanks to all participants and congrats to the winners! You can contact me by sending an email to [email protected].

SPONSORED

ViRE is a Visual Regular Expressions tool: Readable Regex • Code Complete / Cheat Sheet • Unit Tests • Powerful Replace System • Step-by-Step Search & Replace • Regex Visual Scheme • Regex History / Playground. ViRE is available on Mac & iPad.

CURATED FROM THE COMMUNITY

CODE

I’ve seen articles about removing the AppDelegate for SwiftUI apps, but what if you want to add it back? Kornel Miszczak explains how this works.
Swift on the server is not often covered in blog posts, so I was happy to see this post by Tim Condon covering the implementation of Elasticsearch using AWS Lambda Runtime.
Implementing Dark Mode support in our apps is something we all do someday (right?). We don’t always offer the option to override this setting instead of using the system setting. Tundsdev tells us how you can implement this using @Binding and @AppStorage.
One of those websites you regret not knowing earlier for those cases you’re stuck with the if case let syntax. Definitely a useful resource to me!
While working on integrating a SwiftUI View into a Collection View header, I run into several problems. This post by Noah Gilmore helped me figure out how to handle self-sizing when embedding SwiftUI views correctly.
I’m not linking this tweet by Khoa because I think this menu is useful for you. I’m linking this because it can be an inspiration for your apps. The Collect by WeTransfer app contains a debug menu, RocketSim has debugging functionality, and other apps I built too. It can help you a lot in testing and building your products.
Combine is not an easy framework so I don’t regret linking to duplicate resources more often. Not long ago I linked to an article from John Sundell on the same, but I feel like you’ll be able to more easily work with Combine, the more you read about it. This time, it’s Majid Jabrayilov explaining to us how you can convert your closure into Combine supporting API, allowing you to use Combine operators accordingly.
A small change with a big impact! I wasn’t aware this was new in Swift 5.4, but I’m definitely aware this is a great improvement. John Sundell explains what this new change is about.

OPTIMISING

Converting JSON into Swift Structs has never been easier. This can be a great time saver if you’re converting your JSON into Swift models.

THANK YOU

Thanks for being a SwiftLee subscriber! 
Continue your journey: