I'm just getting started with Combine. I have these questions for this situation:
Example situation:
A SwiftUI log in view where I hand off logging in to another class and expect a result back:
struct LogInView: View {
var loginSubject = PassthroughSubject<(username: String, password: String, completion: (Error?) -> Void), Never>()
var body: some View {
Button {
loginSubject.send((username: "Jim", password: "qwerty123", completion: { error in
if let error = error {
// handle error
} else {
// navigate to app
}
}))
} label: {
Text("Log in")
}
}
}
I would like to know other possible solutions to this scenario (I'm not able to use the 'login helper' class directly in the SwiftUI view due to 'LogInView' being in a package and the 'log in helper' being in the main app) and/or if this would be generally accepted as a solution.
ObservableObject
that you inject into your view hierarchy as an @EnvironmentObject
.Further reading: https://www.hackingwithswift.com/quick-start/swiftui/how-to-use-environmentobject-to-share-data-between-views