View profile

 iOS Code Review | Curated code improvement tips - Issue #6

 iOS Code Review
 iOS Code Review
Hi there,
How is your Thursday going?
iOS 15 GM is right around the corner, people are buzzing about last moment API changes and additions. And I’m here with some good old code improvement tips 😀👇

Waiting on multiple async calls
Have you seen code that’s running multiple async calls in a loop, calling completion on the last item in the loop? Or counting the number of callbacks called? I’ve seen it a lot, and the last time has prompted me to share this tip 🙂
DispatchGroup allows you to wait on multiple async operations, as complex as they may be. The concept is simple: the dispatch group keeps track of how many tasks are outstanding (unfinished). enter() and leave() calls increment and decrement the internal counter. Once the counter reaches zero, your custom closure is called. Here’s a small example by @joemasilotti:
Joe Masilotti 📗
Whoa, DispatchGroup is kind of magic! I'm assuming this has been superseded by something even fancier in Combine though, right? https://t.co/gP7HQ882XF
Conditional modifiers are bad? 😬
Are you using conditional modifiers in SwiftUI? I am 🙈 Folks at @objcio came out with a detailed explanations on why it’s not such a good idea. SwiftUI uses the type system for diffing of state, and conditional modifiers such as applyIf(...) break that.
The article also dives into why SwiftUI works the way it does, so check it out if you want to learn more: Why Conditional View Modifiers are a Bad Idea
objc.io
We keep seeing people recommend conditional view modifier helpers. These will break animations and state properties. By understanding why they break things, we'll learn some interesting things about SwiftUI as well! 🧐

https://t.co/aFu3r80JbG https://t.co/89CGiS8eHl
Timers, scrolling, run loops
When you use Timer.sheduledTimerscheduledTimer(withTimeInterval:...), it will not trigger while the user is interacting with a scroll view, or is performing any other touch-tracking interaction. The timer will continue after the interaction has ended. That’s because this convenience method auto-schedules the timer in the .default runloop mode, and the default mode doesn’t check the timer during touch interactions.
If you want the timer to continue as normal during touch events, you’d have to init and schedule the timer with .common mode manually: RunLoop.current.add(timer, forMode: .common)
@qdoug shared a handy cheat-sheet for remembering which mode to use, as their names are super confusing:
Douglas Hill
When using Timer or CADisplayLink I’m always confused if I want the common or default RunLoop Mode. Summary:

.default — only runs when touches are not being tracked
.tracking — only runs when touches are being tracked
.common — runs whether touches are being tracked or not
Review the resources too
A fascinating anecdote shared by @krzyzanowskim. 98% of an app’s size is icons, 11 MB each. That’s nearly 1600x1600px. Why a png icon need to be this big? I bet this is not intentional, and these folks can reduce their app size at least 5x by including appropriately-sized images.
AKA this is a reminder to review the assets that are being introduced to the app, and not only code 🙃
Marcin Krzyzanowski
I've been quite surprised by the size of the app 332 MB (native macOS app) in total. Then I learned icons resources take 312 MB? well... but keep bashing Electron apps for a size. https://t.co/e7wKj6BJ96
Quick 2-in-1 tip
1) NSCache exists and is a good alternative to a plain dictionary when you want to cache non-trivially sized items
2) How feedback is delivered is extremely important. Simply being friendly goes a long way ✨
Trevor Brindle
Today I learned:

You can see time based comparisons in git (and in github at github​.​com/${owner}/${repo}/compare)

Use this syntax for the branch name:

`[email protected]{2hours}`
`[email protected]{10days}`
`[email protected]{1month}`

`git diff [email protected]{2hours}...latest` https://t.co/gQCylm7ssT
Pitt (Swift && Tips)
There is always a way to improve our code or anything, and also better ways to give feedback. Don't forget to be polite! https://t.co/tg4degOe1R
🤘
Alright, that’s it for today. 
Did you enjoy this issue? Let me know by pressing the buttons below, so I can improve the newsletter. 
Got feedback? Want to see more, or less of certain kinds of tips? I’d love to hear from you. Reply to this email or reach out on Twitter via @ios_code_review 🙌
Did you enjoy this issue? Yes No
 iOS Code Review
 iOS Code Review @ios_code_review

Bi-weekly newsletter amplifying code improvement tips from the Apple developer community. Swift, Objective-C, iOS, macOS, SwiftUI, UIKit and more. Curated by Marina Gornostaeva and published every other Thursday.

In order to unsubscribe, click here.
If you were forwarded this newsletter and you like it, you can subscribe here.
Created with Revue by Twitter.