Mobile: background tasks

Mobile: background tasks

  

We are creating a mobile app that needs to continuously access a REST service (3rd party) using long polling. What's the typical way of doing that in an OS mobile app? We've looked at web workers, but I'm not sure if they integrate well with the Platform's mobile apps.

Hi Kilian,

To create that polling mechanism you can either use setInterval or web workers. Either way you'll have to use JS to perform that task. Be aware that web workers have an extra degree of difficulty: you can only pass strings to web workers, so you will have to serialise all the data before.

Probably you can go with setInterval and call a platform action that does the call to the rest service. This action can be asynchronous so it will not block the UI thread for too long (as long as you don't do any long duration work there).

Hi João,

Im currently using setInterval, but I'm not sure whether this can't block the UI thread; as I wrote, we're having to use long polling, which means that the REST call may take a considerable amount of time (about 10 seconds). Also, the downside of setInterval is that it is called at a fixed time, which may call synchronization problems between the interval and the polling interval (I could probably use setTimeout for this though).

As for web workers, creating a web worker means I have to specify a specific .js source file. Can I just add a Script to the eSpace and use that?

Solution

Doing a REST call is async so it won't block the UI thread for much time.

As for web workers, you can do it, I don't foresee any problems with that, although I never tried that on the platform.

Solution

Ok, thanks João, we'll give it a try!

Hi João,

One additional question: since a web worker needs a seperate JavaScript file, am I correct in assuming that means that calling (mobile) actions etc. is not possible?

Probably you could require the global controller file with the global client actions and call them, but I do not recommend since its not supported and might break in further versions.

I would reconsider the setInterval approach - as long it is just a REST call and some very lightweight processing it shouldn't have great impacts in the UX.