SwiftUI+Combine -动态订阅出版商字典



在我的项目中,我持有通过grpc流更新的项目的大字典。在应用程序中有几个地方我渲染这些项目到UI,我想传播实时更新。

简化代码:

struct Item: Identifiable {
var id:String = UUID().uuidString
var name:String
var someKey:String

init(name:String){
self.name=name
}
}
class DataRepository {
public var serverSymbols: [String: CurrentValueSubject<Item, Never>] = [:]

// method that populates the dict
func getServerSymbols(serverID:Int){
someService.fetchServerSymbols(serverID: serverID){ response in
response.data.forEach { (name,sym) in
self.serverSymbols[name] = CurrentValueSubject(Item(sym))
}
}
}
// background stream that updates the values
func serverStream(symbols:[String] = []){
someService.initStream(){ update in
DispatchQueue.main.async {
self.serverSymbols[data.id]?.value.someKey = data.someKey
}
}
}

}

ViewModel:

class SampleViewModel: ObservableObject {
@Injected var repo:DataRepository   // injection via Resolver
// hardcoded value here for simplicity (otherwise dynamically added/removed by user)
@Published private(set) var favorites:[String] = ["item1","item2"]
func getItem(item:String) -> Item {         
guard let item = repo.serverSymbols[item] else { return Item(name:"N/A")}
return ItemPublisher(item: item).data
}
}
class ItemPublisher: ObservableObject {
@Published var data:Item = Item(name:"")
private var cancellables = Set<AnyCancellable>()

init(item:CurrentValueSubject<Item, Never>){
item
.receive(on: DispatchQueue.main)
.assignNoRetain(to: .data, on: self)
.store(in: &cancellables)
}
}

带子视图的主视图:

struct FavoritesView: View {
@ObservedObject var viewModel: QuotesViewModel = Resolver.resolve()
var body: some View {
VStack {
ForEach(viewModel.favorites, id: .self) { item in
FavoriteCardView(item: viewModel.getItem(item: item))
}
}
}
}
struct FavoriteCardView: View {
var item:Item
var body: some View {
VStack {
Text(item.name)
Text(item.someKey)   // dynamic value that should receive the updates
}
}
}

我一定是明显错过了一些东西,或者这是一个完全错误的方法,但是我的Item卡没有收到任何更新(我验证后端流是活跃的,serverSymbols字典正在更新)。任何建议将不胜感激!

我已经意识到我犯了一个错误-为了接收更新,我需要传递ItemPublisher本身。(我错误地返回ItemPublisher。数据从我的viewModel的方法)

我已经重构了代码,并使ItemPublisher使用item键直接从我的存储库中提供数据,因此现在每个卡片都使用publisher单独订阅。

最终工作代码:

class SampleViewModel: ObservableObject {
// hardcoded value here for simplicity (otherwise dynamically added/removed by user)
@Published private(set) var favorites:[String] = ["item1","item2"]
}

MainView和CardView:

struct FavoritesView: View {
@ObservedObject var viewModel: QuotesViewModel = Resolver.resolve()
var body: some View {
VStack {
ForEach(viewModel.favorites, id: .self) { item in
FavoriteCardView(item)
}
}
}
}
struct FavoriteCardView: View {
var itemName:String
@ObservedObject var item:ItemPublisher

init(_ itemName:String){
self.itemName = itemName
self.item = ItemPublisher(item:item)
}
var body: some View {
let itemData = item.data
VStack {
Text(itemData.name)
Text(itemData.someKey)   
}
}
}

最后,修改ItemPublisher:

class ItemPublisher: ObservableObject {
@Injected var repo:DataRepository
@Published var data:Item = Item(name:"")
private var cancellables = Set<AnyCancellable>()

init(item:String){
self.data = Item(name:item)
if let item = repo.serverSymbols[item] {
self.data = item.value
item.receive(on: DispatchQueue.main)
.assignNoRetain(to: .data, on: self)
.store(in: &cancellables)
}       
}
}

相关内容

  • 没有找到相关文章

最新更新