UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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:
              changedToBackground()
          case .inactive:
              changedToInactive()
          case .active:
              changedToActive()
          @unknown default:
              break
          }
      }

2      

@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.

2      

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.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.