Hi there. Did you know that it's already August?
In today's issue, among other things, @v_pradeilles shares two video tips, memory leaks in Combine make a come-back, and we fix Copilot's code. Let's dive in 👇
When not to use [weak self]
@swiftandtips found a well-structured writeup on when to use and not to use [weak self] in closures. Here's a concise chart to help you decide.
If you also want to learn the why's, read the full article: You don’t (always) need [weak self] by @BesherMaleh
Lazy collections for speed
Love the new tip series by @v_pradeilles. In this short video he demonstrates speeding up his code 400.000 times just by adding .lazy. Yes, only five characters 🔥 Watch full video on Youtube (6 min)
There's no secrets in UserDefaults
And another tip from @v_pradeilles. You've likely heard that saving sensitive data to UserDefaults is a bad idea, but perhaps weren't sure why?
Vincent recorded a short tutorial demonstrating how the data saved in UserDefaults can be easily accessed. Watch on Youtube (4 min)
Memory leaks in Combine
We've touched on this topic in the Issue #3. Well, there are always more places to find retain cycles in.
If you're binding a publisher's values to a property of a class, watch out for which operator you use. Using .assign(to: \.property, on: self) will retain self and may cause a retain cycle. On the other hand using .assign(to: $property) retains the property's publisher, not self.
This old forums thread has an example of leaking code. Know your enemy!
Does 'assign(to:)' produce memory leaks? - Using Swift - Swift Forums
Hello there! I'm playing around with Combine and I've found a possible memory leak when using 'assign(to:)' to store stream result in current object. Let me show a couple of examples using 'assign(to:)' and 'sink' to co…
In Issue #2 I asked for your suggestions on what can be improved in Copilot's code, and you sent some great suggestions! There are many things that could be improved there. Did you notice that it doesn't even compile?
I made a playground with four improvement iterations. Check it out to see if you've spotted everything, or if there's anything we missed 👀
Did you enjoy this issue? Let me know by pressing the buttons below, so I can improve the newsletter.
Got feedback? I’d love to hear your thoughts! Reply to this email or reach out on Twitter via @ios_code_review 🙌