[architecture] How do you approach global reference data?

I’ve got an app that relies on a set of data that doesn’t change across the life of the app. It may get updated in between sessions but generally speaking, it’s a set of reference data that’s used to populate various selects and other form controls.

What’s your usual approach for loading these once and only across an app session and making them available in several areas of your apps?

Right now I have an “Initial Data” factory but due to where it needs to sit in the app, it’s getting called more than once per app user session, wasting bandwidth and slowing the app down.

I ussually create a service and hold the data in the service.

I’m doing that too but the issue is the main call to these ref data services has to happen after login on the main view. The user returns to this view often and so this service gets called each time as it’s on a resolve (with promises). I have to have this view uncached too as it needs to refresh itself each time the user comes to it.

I’m now experimenting with cacheFactory to improve things a little but I’m unsure when to hook into a cache clear to ensure the data gets refreshed from time to time i.e. once per app session.

I guess i could also hook onto $ionicView.leave so that it’s called after leaving the login screen…

This does not prevent you from storing this data in a local variable of your service so that when your service is called it tests if variable is defined and returns its value or calls your backend.

1 Like

so only do the actual call to the server once right after login and use the data from server in de servce class as local data.

I do something like this

  1. I have a function in my service class , lets say “getDataFromServer” … I store the data from the server in my service class

  2. The service class has a function getData, that just returns the data in the class.

So right after the real login action, I do “getDataFromServer” and if not just “getData”

1 Like