Comments (4)
Hey @Cobrijani
Thank you for the feedback! Regarding your question, the state management in SwiftUI always complies with the "single source of truth" principle, so you'll need to choose the "owner" for this Loadable.
You have three options: parent component, child component, and AppState.
The AppState would make it accessible to the rest of the app - useful for some cases, but certainly not for all.
With regards to the parent - child parity, I'd bet that most of the time the parent should be the owner, and the child should consume the value through Binding. This way both parent and child will see the changes in the Loadable while either one can kick off the loading process by supplying the Binding to the Interactor (Service).
from clean-architecture-swiftui.
Hi @nalexn
Thanks for your answer! It completely makes sense to choose parent as owner in this case!
How would you implement parent ownership in case of view model approach?
Is LoadableSubject instead of Loadable needed in the view model in order to get the binding?
I tried creating Loadable Publisher in the parent and passing it to the child, but the updates in the parent are not being triggered when the data load is being triggered in the child view.
Pseudo Code Snippet:
struct CountriesList: View {
@ObservedObject private(set) var viewModel: ViewModel
var body: some View {
// in this case child will trigger the loading of the countries in this loadable
// parent will pass .notRequested
Child(countries: self.viewModel.countries)
}
}
extension CountriesList {
class ViewModel: ObservableObject {
// ...
@Published var countries: Loadable<LazyList<Country>>
init(...){
// inits ...
// can something like this work? Do i need loadable subject store above and not just publisher?
$countries.sink {
// register when child loads the component to show something in the parent
}.store(cancelBag)
}
}
Thanks!
from clean-architecture-swiftui.
Ok, the Child
should consume the Binding
in the init:
struct Child {
@Binding var value: Int
init(value: Binding<Int>) {
self._value = value
}
}
And you should pass the binding from the parent this way:
Child(countries: self.$viewModel.countries)
Note that self.viewModel.$countries
will give you a Publisher
, not a Binding
.
I recommend reading my other post where I cover these topics.
from clean-architecture-swiftui.
Hi @nalexn,
Great, thanks!
Best regards
from clean-architecture-swiftui.
Related Issues (20)
- Typo: TextField spelling HOT 1
- Cannot connect to REST API HOT 4
- How to save related CoreData objects? HOT 1
- Something wrong with @Builder and @resultBuilder !!! HOT 1
- How to get value from LoadableSubject? HOT 1
- Why does the '.onReceive(routingUpdate)' perform twice? HOT 1
- Project does not build in Xcode 13.2; Internal error: missingPackageDescriptionModule HOT 2
- Question: why `case .isLoading` is not called even we got `.isLoading` on didSet? HOT 1
- Find by AccessibilityIdentifier HOT 3
- Reseting route state HOT 5
- app crash after select some countires HOT 1
- Would be cool to have the same using SwiftUI Lifecycle
- safearea is not correct HOT 2
- How do I add pagination?
- Question regarding Value: Decodable and Closures
- How do i add interceptor? HOT 1
- Few questions
- CancelBag Strong Reference Cycle HOT 7
- Decouple `Respository` logic HOT 1
- DIContainer conforming EnvironmentKey - can't find its usage HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clean-architecture-swiftui.