AsyncReactor is a reactive architecural pattern to organize your Swift code.
AsyncReactor is a reactive architecural pattern. The main component is an AsyncReactor
. This AsyncReactor
holds the State
as well as the Actions
.
With iOS 17 and later, we introduced the new Reactor
package, which includes a protocol also named Reactor
.
Thanks to the new @Observable
macro in Swift, there’s no longer the need to explicitly mark the State
as @Published
. By addibg the @Observable
macro to your reactor class, the entire class is now automatically observable. If you want to exclude specific properties from observation, you can use the @ObservationIgnored
macro.
The way actions are handled remains the same as before — simply define an Action
enum and implement the action(_:)
method asynchronously.
✅ Both versions of the Reactor (pre-iOS 17 and the new iOS 17+ version) can coexist in your app, as they are implemented in separate packages.
import Reactor
@Observable
public class CatFactsReactor: Reactor
public enum Action {
case loadCatFact
}
@Observable
public class State {
var fact: AsyncLoad<String> = .none
}
public private(set) var state = State()
@ObservationIgnored
private var catFactService: CatFactService
public init() {
send(.loadCatFact)
}
public func action(_ action: Action) async {
switch action {
case .loadCatFact:
state.fact = .loading
do {
let fact = try await catFactService.getRandomCatFact().fact
state.fact = .loaded(fact)
} catch {
state.fact = .error(error)
}
}
}
class RepositorySearchReactor: AsyncReactor {
enum Action {
...
}
struct State {
...
}
@Published
private(set) var state: State
func action(_ action: Action) async {
...
}
}
The AsyncReactor
is provided to the child view as an EnvironmentObject
. This is set by the ReactorView
.
ReactorView(RepositorySearchReactor()) {
RepositorySearchView()
}
Now the reactor can simply be used in the SwiftUI view as follows.
struct RepositorySearchView: View {
@EnvironmentObject
private var reactor: RepositorySearchReactor
var body: some View {
...
}
}
Whenever the State
in the reactor changes, the view will updated accordingly. It is also possibel to bind the State
.
var body: some View {
Text(reactor.state.name)
}
Actions are used to trigger a behaviour in our reactor.
var body: some View {
Button("Click me") {
reactor.action(.buttonClick)
}
}
Action bindings enable us to bind values of the state in our view.
struct RepositorySearchView: View {
...
@ActionBinding(RepositorySearchReactor.self,
keyPath: \.hidePrivate,
action: .onHidePrivateToggle)
private var hidePrivate: Bool
var body: some View {
Toggle("Hide Private Repos", isOn: $hidePrivate)
}
}
class RepositorySearchReactor: AsyncReactor {
...
func action(_ action: Action) async {
switch action {
case .onHidePrivateToggle:
state.hidePrivate.toggle()
...
}
}
}
An example can be found in the Example folder.
MIT License
Copyright (c) 2023 DIAMIR Holding
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.