[Solved] SwiftUI+Combine – Dynamicaly subscribing to a dict of publishers

mike_t Asks: SwiftUI+Combine – Dynamicaly subscribing to a dict of publishers
In my project i hold a large dict of items that are updated via grpc stream. Inside the app there are several places i am rendering these items to UI and i would like to propagate the realtime updates.

Simplified code:

Code:
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:

Code:
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) -> Symbol {         
        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)
    }
}

Main View with subviews:

Code:
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))
            }
        }
    }
}

Code:
struct FavoriteCardView: View {
    var item:Item
    var body: some View {
        VStack {
            Text(item.name)
            Text(item.someKey)   // dynamic value that should receive the updates
        }
    }
}

I must’ve clearly missed something or it’s a completely wrong approach, however my Item cards do not receive any updates (i verified the backend stream is active and serverSymbols dict is getting updated). Any advice would be appreciated!

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.