-1

It's more of a theoretical question, since I don't have the code at hand, but I think it can be anwsered nevertheless.

I have a service (MsgService) that is responsible for supplying msg-s to components. When I update/set msg-s of the service from one of the components with a method similar to this one:

myMethod(data) {
        this.myMethodSubject.next(data);
    }

the new msg-s are not visible rigth away, but only after I refresh the page. I'd like to see the results of calling next immidiately on all the components (that are subscribed to it) and views, but I need to refresh the page once to make the results visible. What can be the reason for that? Or to put it more generally - under what circumstances are the results of Next and subscribtion visible rigth away?

(BTW:I'm calling the subscribe method that gives me the msg-s from NgOnInit, should I put it somewhere else?)

Edit: my subscription from one of the components looks something like this:

ngOnInit() { this.msgService.getMsgParams().subscribe(data => { this.params = data; }); 

this.msgService.getMsgs (this.params.category, this.params.price, this.params.title).subscribe(data => this.msgs = data); 
}

So far, I haven't yet unsubscribed, is that connected with the issue?

Mandy
  • 107
  • 1
  • 2
  • 9
  • 3
    How are you handling the subscribe? The data should be available immediately. Do you unsubscribe as well? Maybe some code from components would offer some insight – Ric Aug 17 '19 at 10:18
  • The most probable reason is that you're not equaling your variable data **into** the subscribe function – Eliseo Aug 17 '19 at 10:25
  • Without code it wont be possible to answer, also what bothers me that after refresh you see that means that you are saving and reloading data. – Vova Bilyachat Aug 17 '19 at 10:25
  • @Ric I'm not unsubscribing yet, can that make a change? I posted the code (at least what I remember of it) above. Thank you for the suggestion! – Mandy Aug 17 '19 at 16:31
  • @Eliseo, I'm not sure I understand what it means to equal into. Does the edited code I posted afterwards do that? Thank you in any case!! – Mandy Aug 17 '19 at 16:35

1 Answers1

1

A view for a component using an async subscription will not update for the following reasons.

  • the view does not use the async pipe to show values from an observable.
  • a subscriber does not mark the view as dirty using markForCheck()

A shared service can not transmit data to components for the following reasons.

  • the service is using a Subject and emitted values are lost before components subscribe. Try using a BehaviorSubject() or a ReplaySubject() instead.
  • the service is not a singleton and the service that emits values is a different instance than the components subscribing. Try using @Injector({provideIn:'root"}) to ensure it is a singleton.

These are the main reasons why people ask the above question. There are other reasons why your data might not be updating, but those reasons tend to be unrelated to rxjs. Assuming this is an observable issue, then I would check the above to see if it resolves your issue.

Reactgular
  • 52,335
  • 19
  • 158
  • 208
  • Thank you very much for your answer! I've already tried most, but will try it again, now that I see I was on the right track. Regarding markForCheck - I'm not (knowingly) using onPush strategy, can it be relevant anyway? Thank you again. – Mandy Aug 17 '19 at 16:29
  • @Mandy you still have to use markForCheck from an async operations weather or not you're using OnPush. There is no way for the component to know that it's state has changed otherwise. – Reactgular Aug 17 '19 at 23:14
  • thank you!! I haven't tried this yet, since I completely misunderstood the docs. Thank you very much again, you've quite possibly saved me from another day of useless attempts. – Mandy Aug 18 '19 at 08:16