devfd / react-native-workers

Background services and web workers for react-native
BSD 2-Clause "Simplified" License
805 stars 89 forks source link

Getting Error on the latest version #3

Closed sudharsan1988 closed 8 years ago

sudharsan1988 commented 8 years ago

Hi @devfd , many thanks for the updated version :) I ran into some issues when I try to use the latest version,

I am using RN 0.30.0 on a Ubuntu 14.04.4 machine

Issue #1: RNPM link gives build error, so I went with manual and that worked for me

Issue #2: When I try to use "new Worker()", it throws an error saying that

Attempt to invoke virtual method 'android.app.Activity.getApplication()' on a null object reference'

Here is what I am doing,

I created a worker.js file on the same directory of my container and called the worker as follows,

`import { Worker } from 'react-native-workers';

/* start worker */ const worker = new Worker("./worker.js");`

Here is the content in my worker.js

`import { self } from 'react-native-workers';

/* get message from application. String only ! */ self.onmessage = (message) => { }

/* post message to application. String only ! */ self.postMessage("hello from worker");`

Am I missing something here?

devfd commented 8 years ago

do you have the same runtime error on the sample app ?

sudharsan1988 commented 8 years ago

I tried the sample and it worked well. I found what I was missing.

1. I was trying to include the below code outside of the component class

`worker.postMessage("hello from application");

/* get message from worker. String only ! */ worker.onmessage = (message) => {

}`

but I changed it to below as per the example,

componentDidMount() { // create a new worker this.worker = new Worker('App/Containers/worker.js'); // receive messages from worker this.worker.onmessage = (message) => { console.log("Got message from worker", message); } }

2. I also made changes to the worker path as per the example

From:

const worker = new Worker("./worker.js");

I changed it to,

this.worker = new Worker('App/Containers/worker.js');

I really appreciate your help and sorry for the trouble :)

I also have another question, it works only when the app is active but I thought "worker" would work even the app is killed. Is there anyway we can make it work in the background even after the app is killed or in active?

devfd commented 8 years ago

on iOS there is no way to keep a worker active while in background. on Android it is possible using Service. The lib will be updated to allow Service creation.

sudharsan1988 commented 8 years ago

@devfd , thanks again for your help. Yes, I am trying to get it done on Android. Very excited to see the updated version with Service creation.