react-native-background-task icon indicating copy to clipboard operation
react-native-background-task copied to clipboard

Expected to run on UI thread!

Open sandropoluan opened this issue 8 years ago • 6 comments

Expected to run on UI thread! I got that error.

My code

BackgroundTask.define(async () => { 
              try{ 
                  navigator.geolocation.getCurrentPosition(
                    async (position)=>{                       
                        fetch(publicUrl+'/posisi_members',{
                          method:'post',
                          credentials:'include',
                          headers:{
                             'Accept': 'application/json',
                             'Content-Type': 'application/json',
                          },
                          body:JSON.stringify({
                            latitude:position.coords.latitude,
                            longitude:position.coords.longitude,
                            test:true,
                          }), 
                        }).then(response=>{}).catch(error=>{});

                    },(error)=>{},{enableHighAccuracy: false, timeout: 20000, maximumAge: 100000});

              } catch (error){

              }    
            ;
})

sandropoluan avatar Oct 22 '17 11:10 sandropoluan

My guess is this would be to do with navigator.geolocation.getCurrentPosition being code which can't be run in the background. If you take out that line, and make the same fetch request with a dummy value for position, presumably it works?

jamesisaac avatar Oct 22 '17 23:10 jamesisaac

I would recommend using a more specific library for this purpose, e.g.: https://github.com/transistorsoft/react-native-background-geolocation

jamesisaac avatar Oct 22 '17 23:10 jamesisaac

I have this problem and I don't use navigator.geolocation at all. "react-native-background-task": "^0.2.1", "react-native": "^0.48.4",

artemukolov avatar Oct 30 '17 07:10 artemukolov

@artemukolov What code are you trying to execute in your task?

jamesisaac avatar Oct 30 '17 20:10 jamesisaac

So. Looks like I fix it by update RN to 49.5. But this requires React 16.beta.5 Be careful.

artemukolov avatar Oct 31 '17 08:10 artemukolov

Has anyone found a different solution for this issue besides upgrading to RN 49.5 with React 16.beta.5? This is my background task:

BackgroundTask.define(() => { console.log('Hello from a background task') BackgroundTask.finish() }).

It looks like it works when the app is in the background, but if I kill it then I get the 'Expected to run on UI thread' error. I tried commenting out the console log but the issue persists.

steferic avatar Jan 04 '18 19:01 steferic