View profile

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

 iOS Code Review
 iOS Code Review
Hi there,
Hope you’re safe, wherever you are. I’m excited to share this week’s findings with you!
This time I’m doing something new in addition to the usual tips. The pandemic has been hash on the conference scene, and the next conference season is coming up. I figured I’d highlight some conferences that are still looking for speakers.
Enjoy! ☀️ vs @MainActor
You can dispatch a Task to the main actor two ways - by explicitly calling or by annotating the closure with @MainActor. These two ways have different behaviour. If already on the main thread, the former will run immediately but the latter will run on the next runloop cycle. Find detailed explanation in this article by Paul Hudson.
Khoa 🔥
If you find yourself using Task then again to use concurrent context for main actor dispatch.

You can just annotate the Task closure with MainActor #iosdev
Łukasz Kuczborski
@onmyway133 It is worth remembering though that code in { } will run immediately if your function is already on the main actor and annotated Task closure will run in the next run loop as described nicely by @twostraws here
Synchronizing user defaults
Since iOS 12, the synchronize() method on UserDefaults does nothing, and we don’t need to call it anymore. It’s interesting that the method is not marked as deprecated yet. Here’s a quote from the release notes for iOS 12:
Removed synchronization requirements. It’s no longer necessary to use synchronize()CFPreferencesAppSynchronize(:), or CFPreferencesSynchronize(:::). These methods will be deprecated in a future version of the OS. Now that you don’t need to call these synchronization methods, the performance characteristics of UserDefaultsand Preferences Utilities are slightly different: The time taken for enqueueing write operations is now paid by the writing thread, rather than by the next thread to call synchronize() or do a read operation.
Jordan Morgan
Name one piece, across Apple's entire documentation suite, that's ignored more:
Codable in Firebase
There’s now built-in Codable support in Firebase, so there’s no need to bring in additional packages such as CodableFirebase. Now you can simply write: ref.getDocument(as: Book.self) {...}. The type just needs to have a property annotated with @DocumentID property wrapper, so the framework knows which property is the identifier. If you want to learn more, Peter also wrote a comprehensive guide.
Peter Friese
Don't map your Firestore documents manually, folks. Tell your friends, too.
On refactoring
Wise words here. If one commit contains both refactoring and behaviour change, if things break (or, when) it’ll be hard to tell whether they broke due to refactoring or due to a bug in new behaviour. I split these up, always.
Also sometimes people call any change in code structure a refactoring. Here’s a quote from Wikipedia: “code refactoring is the process of restructuring existing computer code—changing the factoring—without changing its external behavior.”
Andrew Clark
Something that's helped me: Don't refactor and change behavior at the same time. Do one and then the other.

Doesn't always need to be separate PRs but should at least be separate commits.
Did you know that apps using iCloud (CloudKit) cannot be transferred to another account? This means that selling an app with CloudKit can be a tricky endeavour - with the only option being to sell the whole developer account.
iCloud red flag for indie developers
Conference time!
This time I want to help the community by highlighting some of the upcoming conferences.
CFP (call for papers) is a format when potential speakers submit their talk ideas. If you’ve been dreaming about giving a talk - this is your hint to finally go for it 🤘
Looking for speakers
iOSDevUK, Aberystwyth, UK, 5-8 September 2022. They’re looking for speakers from underrepresented groups.
SwiftLeeds, Leeds, UK, 20 October 2022.
360idev, Denver, US, 28-31 Aug 2022.
SwiftConf, Cologne, Germany, Aug 17-18, 2022.
New kid on the block
plSwift - A brand-new Swift/iOS conference in Wrocław, Poland. 25-26 May 2022.
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 in a bite-sized format. Swift, Objective-C, iOS, macOS, SwiftUI, UIKit and more. Curated by Marina Gornostaeva and published every other Thursday.

For feedback or sponsorship ->> [email protected]

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.