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

Using observable objects etc.

Forums > SwiftUI

I have an app that has two optional strings, each holds a userid. when the app initialises i get them, if present, from user defaults. The app displays them on the main screen. I also have a screen to allow me to set and / or clear these strings and saves them in userdefaults.

now what i would like to do if when i leave the screen that sets the userids, i want to update the main screen to show the changes. At present, it does not, and only displays the updates when i restart the app.

elsewhere the app users these userids as parameters to get data from the internet.

i think i need to publish the changes, but an unsure how to go about it. any ideas?

1      

Mr Watson lays out his app's requirements:

  1. Optional Strings
  2. User Defaults
  3. Edit values
  4. Update on multiple screens.
  5. Get data from internet
  6. Save changes

He asks:

i think i need to publish the changes, but am unsure how to go about it. any ideas?

Cupcake Corner

@twostraws has a great tutorial called Cupcake Corner. From start-to-end this tutorial provides a recipe for collecting data from the user, updating multiple screens, saving to device, and as a bonus, sending the data to a remote server. And, you're right! He uses the @Published property wrapper. Sweet!

Seems like this is exactly what you're asking!

See> Cupcake Corner

1      

thanks, i will check the cupcake app out, i did do that app a while back , but had forgotten about it.

1      

Because you need the user id to be persistent, this is a perfect scenario to utilize CoreData. @twostraws has an excellent tutorial with a sample app in the free 100 days of SwiftUI course that will help you with this.

An ObservableObject class stores the data in memory and when the app is quit that data is lost. If that's what you want, then sure, create a new Swift file and write something like the following:

import Foundation

class MyUserID: ObservableObject {
    @Published var userID = ""
}

Then in your view you simply type:

@ObservedObject var myUserID: MyUserID

However, I don't think this is what you actually want because the userID would be lost each time the app is launched/quit. By storing it in CoreData you can then add to the data stored down the road making it a much more flexible solution.

Good luck!

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.