SwiftUI - Application data / logic architecture

I’m fairly new to native development (excluding react-native) and I’ve been playing around with SwiftUI which I’m finding much better than my experience the traditional imperative approach of building apple applications (UIKit, Storyboards, MVC). I have no problems with building out nice views that match my designs with SwiftUI but the problem I currently face due to lack of native experience is where and how to handle application logic.

I always aim to have a single source of truth (redux in past applications) and have my views dispatch actions to modify the state in some way and I feel like this is what I need for my SwiftUI applications too however I just can’t figure out the best way to approach this.

For example, I have a simple logic screen, user enters email and password then presses login. This buttons action needs to send the network request (I’m guessing URLSession is what I need here, still not done any networking yet), then if it succeeds I need to store the users access token, most likely in the SwiftUI environment and then navigate to another screen in the app.

I’m really just stuck on the integration side of things now, fetching/posting data, reading from device filesystem, event handling etc…

So far from piecing things together I have the following which I’m almost happy with just a few bits I’m lost with (lines 27 and 28)

Maybe I could store the token in the UserDefaults, is this standard practice for iOS apps?

Could someone please point me in the right direction on where to go next, I keep seeing a framework/library called Combine popup lately, would this be helpful for what I need or is that further down the line?

Thank you :slight_smile:

hi @hendore,
first Combine, it is a library from Apple that helps you work with the Publisher/Subscriber model (what is called Observer/Observables in React) . This is from Apple and works well with both UIKit and SwiftUI, so this will be more lucrative to developers in my opinion.

Where do you store the token, you can store that in the UserDefaults as you suggested, you can persist it to the device storage or you can even store it in the Keychain if you want.

cheers,

This topic was automatically closed after 166 days. New replies are no longer allowed.