Vinay B R
Vinay B R

Reputation: 8421

implementing long running background tasks in phonegap

I am trying to run a snippet of javascript which keeps polling the server for updates. what is the best way to get this working with minimal impact on application performance from the end user perspective.

I tried spawning it off in a separate iframe but the resultant performance was pretty bad.

I am currently working on android so if the solution involves writing a phonegap plugin to poll the server instead of javascript that should be fine for now. I need to update data in local storage after polling the server.

Upvotes: 1

Views: 3567

Answers (2)

dhaval
dhaval

Reputation: 7659

If you are planning to poll only when the application is in use the setInterval() works fairly well. My application is using jQuery Mobile which by default only updates DOM rather reloading entire page, that way my setInterval() keeps running once started.

Android already has a support of Background services, which you can use for the long and continuous running tasks.

One solution which I have used with above combination:

  • when the app is running I use setInterval() to check for update periodically.
  • Background checks whether application is running or not before starting the update checks
  • If the app is not running then I use the background service code just to check whether there are any updates on server or not
  • If there are updates then I put a system tray notification to notify user that there are updates waiting for him
  • As soon as he starts the application, I call the service to fetch the updates.

This way my main logic remains in the Javascript side and I need not to write same logic in both Javascript and native again.

If updates are not very frequent then you should consider using PushNotification

Upvotes: 2

10s
10s

Reputation: 1699

I have implemented this in IOS. The problem with phonegap is that you can't create a timer task that runs javascript because if your .html page changes the task would be killed.

Unless you just have ONE .html and you are dynamically changing the body part. If that is the case then look up setInterval in javascript.

If that isn't the case then implementing something on javascript isn't a solution, in IOS I have implemented a custom plugin for running asynchronous web service calls. You could do something similar, writing a plugin that runs a AsyncTask doInBackground() and then postback the results in the javascript. There are many tutorials on how to create a custom plugin in phonegap. Hope it helps

Upvotes: 2

Related Questions