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

   

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

SPONSORED Catch bugs as soon as they happen and know exactly why a crash occurred. Instabug's SDK grabs all the logs they need to fix bugs, crashes and performance issues in minutes instead of days. Get screenshots, device details, network logs, repro steps, and tons of other critical insights needed to resolve issues and prioritize product backlogs straight from your dashboard. It only takes a minute to integrate!

Get started now

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.