Observable only returns cached data

Hello there!
I have a problem with page caching & observables.
Lets say we have a “Page A”, where some data gets displayed, a “Page B” where the user can change some of that data and a “dataService” with an BehaviourSubject and an observable based on that subject.
When the user first visits Page A, then changes some data on Page B, on Page A the subscription only returns a cached data value, so that the user cannot use his changed data.

With vanilla Angular, this works totally fine. But with ionic, my subscriptions are basically useless. Have I missed some core ionic stuff? This can’t be intended behaviour, can it?

can you share repo with minimal code to reproduce

1 Like

Unfortunately, I can’t reproduce this within a new project. Very strange. Maybe it’s not ionics fault and I have something screwed up. I will investigate this further.

You can look into the (relatively small) main project, if you like: https://github.com/JensUweB/Simple-Attendance
Or directly test the app here: https://dashboard.ionicframework.com/app/9f895bb1/preview/7025237

Several things concern me here, but the biggest one is that every service is carrying two separate copies of information. There is that proverb about how a man with two watches never knows what time it is, and I’ve been bitten by that more times than I can count.

I recommend getting rid of all the direct arrays in the services, leaving only the BehaviorSubjects. I typically use a watch / peek / poke idiom as described here, and you should be able to adapt that to your situation.

I suspect this is the cause of your initial problem, because you are likely accessing different copies of the data in different places and misinterpreting what is happening as some sort of caching.

A more minor concern is that there’s no call to use localStorage for anything. Whatever you’re trying to do, better options exist. Ionic Storage has a very similar capability and API, better persistence guarantees, and eliminates the need to manually marshal things into JSON.