'Generic Sensor API in Web Worker

I'm working on a Progressive Web App with Ionic 4. I'm able to use Generic Sensor API in the main thread, but I want to use it in a Web Worker, to read device motions (accelerometer, gyroscope, ...) with an event listener in background, when the app is not active (but opened in background).

Is there a way to do that ? Or is it an actual limitation of PWAs ?



Solution 1:[1]

No, it's not possible to access a Sensor API from Web Workers.

This is because Web Workers can not interact with the user and some API might ask for the user's permissions.

Workers are limited to a small subset of APIs which can be found at MDN: Functions and classes available to workers

Some of them include the

  • messaging APIs
  • limited Navigator API
  • fetch API, XMLHttpRequest and WebSockets
  • Data storage
  • ...

See also: Web Worker API

However, what you can do is, listening to for sensor data in the main thread and sending it via the messaging APIs to the Web Worker.

Solution 2:[2]

You need to delete the migration files and again run the python manage.py makemigrations command. When it Prompts for a one-off default, you can use timezone.now, in order to populate the pre-existing rows in the database.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 try-catch-finally
Solution 2 Swapnil Chopra