0

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:

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

Main View with subviews:

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
        }
    }
}

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!

mike_t
  • 2,484
  • 2
  • 21
  • 39
  • Might not be the *only* issue, but it seems like instead of `self.serverSymbols[data.id]?.value`, you should be using `.send()`. Is `Item` a `struct` or `class`? – jnpdx Aug 02 '21 at 16:57
  • Item is a struct, i've edited the question to add the structure – mike_t Aug 02 '21 at 17:07
  • Can you include a [mre]? There's not enough here to test your code – jnpdx Aug 02 '21 at 17:09
  • It's a rather complex setup with a background thread updates via server side streams, that's why i only posted the simplified relevant parts to illustrate the issue.. I'll try to see if i can tear it down enough for a MRE. – mike_t Aug 02 '21 at 17:15
  • I'm not surprised that this doesn't work. Why do you expect the `item` in `FavoriteCardView` to be automatically updated? It would be extremely helpful if you describe what you expect to happen in specific detail. – Peter Schorn Aug 02 '21 at 17:19
  • @PeterSchorn - i've just realised my silly mistake, i need to pass down `ItemPublisher` and make it `@ObservedObject`, then its value will propagate the updates.. Question still remains if this is the correct approach? – mike_t Aug 02 '21 at 17:25
  • That's the most obvious issue I saw. It doesn't make sense to create an `ItemPublisher` but then immediately access `data` and not retain the `ItemPublisher`. – Peter Schorn Aug 02 '21 at 17:32

1 Answers1

0

I've realised i've made a mistake - in order to receive the updates i need to pass down the ItemPublisher itself. (i was incorrectly returning ItemPublisher.data from my viewModel's method)

I've refactored the code and make the ItemPublisher provide the data directly from my repository using the item key, so now each card is subscribing individualy using the publisher.

Final working code now:

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

}

MainView and 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)   
        }
    }
}

and lastly, modified 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)
        }       
    }
}
mike_t
  • 2,484
  • 2
  • 21
  • 39