Comments (3)
Yeah, you're right @hoc081098, that is how SwiftUI work. I understand that @huyparody get the sample code from README.md, it is my fault, it works perfectly in UIKit where the binding is invoked once, it would be a problem in SwiftUI as demonstrated. I'll update the README.md then.
Suggestion from @hoc081098 is perfect, I just wanna give @huyparody one more approach by using @Published
class ViewModel: ObservableObject {
...
@Published var errors: Errors?
init() {
errorIndicator.errors
.sink { error in self.errors = error }
.store(in: &bag)
}
...
}
Now you can use errors
like other outputs such as isEnableLoginButton
, it is more friendly with SwiftUI rather than Publisher
I'll close the issue, thank guys for the great discussion!
from activityindicator.
Hi @huyparody, thank for your report.
I can reproduce the issue from your sample project.
I just figure out if I sink
the error publisher inside the ViewModel
then the error publisher still works as expected.
I suppose the onReceive
method of the View
in the SwiftUI
works unexpected, unfortunately I don't have much working experiences with SwifUI
then I can't debug further. Hope you can help to figure it out from this point.
from activityindicator.
Every time ContentView.body
is called, viewModel.errorPublisher
will return a new Publisher instance (because it is a computed property).
That causes onReceive(_:perform:)
to cancel the old Publisher and subscribe to the new one. The new one has the same behavior as ConcurrentValueSubject
(aka. @Published private var relay: Error?
~ replay=1
). So the latest error value is replayed the ContentView
is rendered.
-> Avoid this by storing the Lazy Stored Properties that would persist between view re-renders.
class ViewModel: ObservableObject {
lazy var errorPublisher: AnyPublisher<Error, Never> = errorIndicator.errors.eraseToAnyPublisher()
}
from activityindicator.
Related Issues (2)
- Property wrapper HOT 3
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 activityindicator.