NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

SOLVED: UltimatePortfolio: 6. Storing tab selection

Forums > SwiftUI

This step suggests we should use the notification center to catch the app going into the background and takes that opportunity to save any pending changes to the data.

Is there a reason to use notification center instead of SwiftUI's version, that appears to do the same thing, which is:

      .onChange(of: scenePhase) { newPhase in
          switch newPhase {
          case .background:
          case .inactive:
          case .active:
          @unknown default:


@twostraws  Site AdminHWS+

Yes: this API isn't ideal on macOS – it doesn't trigger when a window moves to the background, because "background" means something else entirely on macOS. I'd love to hear alternatives that work well on all platforms, but right now watching for a platform-specific notification seems to work best.


Thank you for your quiuck response. I appreciate it. I will change my code to what yoou showed. I assumed you had a reason, or I thought maybe this API was not available when you recorded the tutorial.

Your course has already answered several of my issues with CoreData and SwiftUI.

Thank you again.


Hacking with Swift is sponsored by RevenueCat

SPONSORED Building and maintaining in-app subscription infrastructure is hard. Luckily there's a better way. With RevenueCat, you can implement subscriptions for your app in hours, not months, so you can get back to building your app.

Try it for free

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.