How to keep functions running across multiple pages

Is there a way to keep functions from one page running after switching to another page? For a very simple example, if I’m running a timer on page 1, is there a way to keep that timer running after I switch to page 2?

I have been storing the processed data from page 1 into local storage, then inject the same functions into page 2 and continue processing the locally stored data.

This is a difficult question to answer in the abstract, but in general everything that is not directly involved with user interaction - including what sounds like making “processed data” and timers - belongs IMHO in a service provider, not a page.

Hopefully that’s all the information you need, because now there are no longer functions attached to pages that you have to worry about.

Incidentally, “local storage” is a very overloaded term. If you are really strictly speaking about this thing, it’s never the right choice for anything. Ionic Storage offers an almost identical API and better persistence guarantees.

At this point, it would probably help to know what the “data processing” consists of. If it’s a long-running and pausable task that can be easily broken into chunks (think bitcoin mining), then it might make sense to use some sort of device storage to allow the app to get paused and resumed while the work is ongoing. In virtually any other scenario, such as modifying a student record retrieved from a network API by enrolling them in a class or changing their legal name, you do not want to use on-device storage here at all. It’s a bad choice for in-app communication because you have to concern yourself with sequencing writes and reads, which is a very tedious and error-prone process.

Right, I for some reason forgot what service provider was called. That’s what I have been doing with Ionic storage. I’m doing some device motion tracking and logging the data. I have been importing the service to every page and restarting the service. It feels tedious and I wasn’t sure if that’s how it’s supposed to be done.

Maybe this is just semantics, but I’ve never had to “restart” a service, so I’m not sure what you mean here.

:thinking: no idea what you are trying to say here but as pointed out you’re better off with singleton services injected at root and use observables to pass around your data and if you really need local storage consider other non-volatile options like SQLite.

What I’m doing now:

  1. Made a service, it make uses of device motion to track some movements and do some calculations based on the movements.
  2. Import the service to every page.
  3. ngOnInit, I retrieve the processed data from storage, and resume my service.
  4. ngOnDestroy, I stop the service and store everything in storage.

It works, but I’m not certain if there’s a standard way to do that.

I’ll read up on how to do that at root.

I use local storage mainly to pass around the data between pages, the final data is stored in a web server.

I guess if you’re happy with it, that’s all that matters. Everything about that sounds completely crazy to me, but that’s probably simply because I don’t really understand what you’re trying to do.

I’m trying to use device motion to log the movements of the phone and do some calculations using the motion data in combination with some other things that I get from a web server. I want the program to continuously track the motion and do the calculations in every page in the app until the user asks for it to stop.

I made it like this, where I’m importing my service into every page, resume the service in ngOnInit, and stop the service in ngOnDestroy.

It just seems very tedious to me to do that on every page. And I was trying to find out if there’s a way to just import that once somehow and just have it somehow running across all pages.

As either name implies, service providers are supposed to “serve” and “provide”, so I don’t think of the concepts of “stop” and “resume” as applying to them at all. I can’t do anything with “do some calculations” and “some other things I get from a web server”. Everything is so vague.

Maybe you would be interested in turning this into a service worker.

While building an Ionic turn by turn tracking app I found these Bing map samples very useful you could have a look and see how you can benefit…

  1. https://github.com/microsoft/Bing-Maps-Fleet-Tracker
  2. Continuously Tracking a User Location Example