如何解决使用SwiftUI /合并,如何避免将可取消对象放入ViewModel
我总是使用RxSwift将DisposeBag放在MVVM的ViewController中,就像本主题中所说的那样:
On iOS,for the DisposeBag in MVVM,can it be placed in ViewModel?
但是使用Combine时,由于View是一个结构,并且不能在其中放置可取消的对象,因此我只能解决问题。
如何在合并中管理View和VM之间的订阅而又不可以在ViewModel中取消添加
或者也许,在SwiftUI / Combine中,没有选择将取消对象放置在VM中。
在SiwftUI / Combine中有一个实现示例:
ViewModel
class EquityViewModel: ObservableObject {
@Injected private var api: AlphaVantageAPI
private var cancellables = Set<AnyCancellable>()
private let code: String
@Published private var result: Quote?
@Published var price: String = ""
init(code: String) {
self.code = code
self.$result
.map {
return "\($0?.price ?? 0) €"
}.assign(to: &$price)
}
func addToPortfolio(){
}
func onAppear() {
self.api.quote(symbol: self.code).share()
.sink { completion in }
receiveValue: { quote in
self.result = quote.quote
}
.store(in: &cancellables)
}
}
视图
struct EquityView: View {
@ObservedObject var viewModel: EquityViewModel
init(viewModel: EquityViewModel) {
self.viewModel = viewModel
}
var body: some View {
ZStack {
Color("primary").edgesIgnoringSafeArea(.all)
VStack {
Text("Stock Price")
.foregroundColor(.white)
.frame(minWidth: 0,maxWidth: .infinity,alignment: .topLeading)
.padding()
HStack {
Text(self.viewModel.price)
.foregroundColor(.white)
Text("+4.75 %")
.foregroundColor(.white)
.padding(.leading,20)
}.frame(minWidth: 0,alignment: .topLeading)
.padding()
Button(action: self.viewModel.addToPortfolio,label: {
Text("Add to portfolio")
.foregroundColor(.white)
.frame(minWidth: 0,maxHeight: 30,alignment: .center)
.background(Color.blue)
.cornerRadius(5)
}).padding()
Spacer()
}
}.frame(alignment: .leading)
.onAppear(perform: self.viewModel.onAppear)
}
}
解决方法
这就是您搜索的内容吗?我无法测试,我不知道您使用的是什么库
class EquityViewMode: ObservableObject {
// @Injected private var api: AlphaVantageAPI
var pricePublisher: AnyPublisher<String,Never>
@Published var price: String = ""
init(){
// init your publisher like
// pricePublisher = self.api.quote(symbol: self.code)
// .share()
// .map { "\($0?.price ?? 0) €" }
// .eraseToAnyPublisher()
}
struct EquityView: View {
@ObservedObject var viewModel: EquityViewModel
var handle: AnyCancellable? = nil
init(m:EquityViewMode) {
viewModel = m
handle = m.pricePublisher.assign(to: \.price,on: self.viewModel)
}
var body: some View{
Text(viewModel.price)
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。