SoloMedium8m

The Settings That Never Saved

#ios-critical — settings randomly reset, 15 orphaned theme observers

QA: Dark mode toggle shows wrong state after reopening Settings. Console prints theme update 15 times per toggle. deinit never called on SettingsViewController.

The trap: Two retain cycles — strong ThemeObserver.delegate and ViewModel closures capturing self strongly.

Your task

Fix `SettingsScreen.swift` in four places: weak delegate, [weak self] in Timer + closures, cleanup in viewDidDisappear.

Book: delegates should be weak · Prod: 15 orphaned VCs corrupt toggle state and drain battery.

Skills you'll prove

swiftiosmemory-managementretain-cycledelegatesweak-referencesclosuresbook-vs-prod

Interview Tip

When asked about delegates or protocols, say: "Every delegate in iOS should be weak to prevent retain cycles. The pattern is always: protocol MyDelegate: AnyObject { ... } and weak var delegate: MyDelegate?. If you forget weak, the delegate holds a strong reference back to the owner, creating a cycle where neither can deallocate. I once debugged a settings screen where settings appeared to randomly reset — the root cause was 15 orphaned ViewControllers all still alive and all fighting to update the same UI because their theme observers had strong delegate references. Adding weak fixed both the memory leak AND the data corruption. My rule: if a delegate relationship is between an owner and an owned object, the delegate must be weak. And always add deinit print statements during development — they are your canary for retain cycles."

Browse freely — sign in when you're ready to open a ticket and prove your skills.