迅捷+;组合框架&x2B;MVVM发布服务器返回空列表
我正在使用Swift用户界面中的MVVM架构与combineframework和Alamofire。 然而,数据被返回到可观测对象,并且正在打印意味着Alamofire和api层中发布的数据,但它不会查看可观测对象中的数据。 我在publisher中打印响应及其打印,但不返回视图 下面是可观察的物体迅捷+;组合框架&x2B;MVVM发布服务器返回空列表,mvvm,swiftui,observable,combine,swiftui-list,Mvvm,Swiftui,Observable,Combine,Swiftui List,我正在使用Swift用户界面中的MVVM架构与combineframework和Alamofire。 然而,数据被返回到可观测对象,并且正在打印意味着Alamofire和api层中发布的数据,但它不会查看可观测对象中的数据。 我在publisher中打印响应及其打印,但不返回视图 下面是可观察的物体 import SwiftUI import Combine class CountryViewModel: ObservableObject { @Published var c
import SwiftUI
import Combine
class CountryViewModel: ObservableObject {
@Published var countryResponse:[CountryResponse] = []
@Published var isLoggedIn = false
@Published var isLoading = false
@Published var shouldNavigate = false
private var disposables: Set<AnyCancellable> = []
var loginHandler = CountryHandler()
@Published var woofUrl = ""
private var isLoadingPublisher: AnyPublisher<Bool, Never> {
loginHandler.$isLoading
.receive(on: RunLoop.main)
.map { $0 }
.eraseToAnyPublisher()
}
private var isAuthenticatedPublisher: AnyPublisher<[CountryResponse], Never> {
loginHandler.$countryResponse
.receive(on: RunLoop.main)
.map { response in
guard let response = response else {
return []
}
print(response)
return response
}
.eraseToAnyPublisher()
}
init() {
countryResponse = []
isLoadingPublisher
.receive(on: RunLoop.main)
.assign(to: \.isLoading, on: self)
.store(in: &disposables)
isAuthenticatedPublisher.map([CountryResponse].init)
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { [weak self] value in
guard let self = self else { return }
switch value {
case .failure:
self.countryResponse = []
case .finished:
break
}
}, receiveValue: { [weak self] weather in
guard let self = self else { return }
self.countryResponse = weather
})
.store(in: &disposables)
// isAuthenticatedPublisher
// .receive(on: RunLoop.main)
// .assign(to: \.countryResponse, on: self)
// .store(in: &disposables)
}
public func getAllCountries(){
loginHandler.getCountryList()
}
public func getAllUnis(_ id:Int){
loginHandler.getCountryList()
}
}
提前谢谢。您能显示查看代码吗?您说收到值的
print
语句在哪里?@NewDev我更新了视图代码并添加了print语句。您需要将属性设置为ObservedObject:@ObservedObject var viewModel:CountryViewModel
非常感谢您的完美!!!!在过去的两天里,我尝试了不同的解决方案。你能显示视图代码吗?您说收到值的print
语句在哪里?@NewDev我更新了视图代码并添加了print语句。您需要将属性设置为ObservedObject:@ObservedObject var viewModel:CountryViewModel
非常感谢您的完美!!!!在过去的两天里,我尝试了不同的解决方案
struct ContentViewCollection: View {
@Binding var selectedCountryId:Int
var axis : Axis.Set = .horizontal
var viewModel:CountryViewModel
@State var countries:[CountryResponse] = []
var body: some View {
ScrollView (.horizontal, showsIndicators: false) {
HStack {
ForEach(self.viewModel.countryResponse) { (postData) in
Button(action: {
self.selectedCountryId = postData.countryID ?? 0
}){
WebImage(url: self.imageURL(postData.countryName ?? ""))
.resizable()
.indicator(.activity)
.scaledToFit()
.frame(minWidth: 40,maxWidth: 40, minHeight: 40, maxHeight: 40, alignment: .center)
}
}
}
}.onAppear() {
self.loadData()
}
}
}