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

BackgroundTimer.stopBackgroundTimer() not stopping timer in iOS

Open davidcunha opened this issue 5 years ago • 17 comments

Hello!

I checked all the other issues related with this and the problem still persists in iOS:

componentDidMount

if (Platform.OS === 'android') {

  this.interval = BackgroundTimer.setInterval(() => {

    this.recurrentTask();

  }, 1000);

} else {

  BackgroundTimer.runBackgroundTimer(() => {

    this.recurrentTask();

  }, 1000);

}

componentwillUnmount

if (Platform.OS === 'android') {

  BackgroundTimer.clearInterval(this.interval);

} else {

  BackgroundTimer.stopBackgroundTimer();

}

It works perfectly in Android but in iOS the timer doesn't stop. I tried the same approach from Android in iOS, which works but not when the app is in background, and I also tried the "Crossplatform" which also doesn't work. This means that the timer is not being released in iOS.

RN version: 0.58.4 react-native-background-timer version: 2.1.1

davidcunha avatar Apr 01 '19 11:04 davidcunha

that correct... not stops in iOs and in android

LuisBonsembiante avatar Apr 01 '19 18:04 LuisBonsembiante

that correct... not stops in iOs and in android

@LuisBonsembiante In Android it works perfectly for me. Android 8.1

davidcunha avatar Apr 01 '19 19:04 davidcunha

In the beginning It looks like it works, but if you close the app and wait 30 - 60 minutes, you will see the battery is drain cause problem with release the wake lock.

If someone know how to release it, it will be very helpful.

I also look at the code and it looks like it should free it, but most of the time is not release it.

roysG avatar Apr 01 '19 20:04 roysG

In the beginning It looks like it works, but if you close the app and wait 30 - 60 minutes, you will see the battery is drain cause problem with release the wake lock.

If someone know how to release it, it will be very helpful.

I also look at the code and it looks like it should free it, but most of the time is not release it.

@roysG Thanks for your comment. I'm only testing with 1min, 3min and 5min in background. For those timespans, well I think there is no solution. The SO is free to even kill the app: https://dontkillmyapp.com/problem

davidcunha avatar Apr 01 '19 20:04 davidcunha

Hi, I think you did not understood me, the app is not killed me. The awake lock is still running in the background and drain the battery, even i close the app.

The second problem, When i turn off the screen there is lag(delay) in the gps location. See also reference here: https://github.com/ocetnik/react-native-background-timer/issues/13

Please assist, thanks. (By the way, awesome library!)

roysG avatar Apr 01 '19 21:04 roysG

Hi, I think you did not understood me, the app is not killed me. The awake lock is still running in the background and drain the battery, even i close the app.

The second problem, When i turn off the screen there is lag(delay) in the gps location.

Please assist, thanks. (By the way, awesome library!)

Oh okay I got it! Yes I didn't know that. So it means that if the app is not closed I still have those locks draining the battery... Ok so it's even worse than I expected.

davidcunha avatar Apr 01 '19 21:04 davidcunha

Yes, my users not happy :( Maybe you know how to fix it?

And also about delayed gps when the screen is off ?

roysG avatar Apr 01 '19 21:04 roysG

@roysG I'm not the developer of this library and I'm not familiar with the codebase. I've just reported a problem that I'm having right now.

davidcunha avatar Apr 01 '19 21:04 davidcunha

oh sorry.

roysG avatar Apr 01 '19 21:04 roysG

@ocetnik , can you assist?

roysG avatar Apr 01 '19 21:04 roysG

same issue, no any solution yet?

chiefchief avatar May 31 '19 08:05 chiefchief

This solution work for me

For Starting timer must do like this.

 startTimer = async () => {
        this.setState(() => ({ timerValue: TIMER_INTERVAL }));
        if (isAndroid) {
            await BackgroundTimer.runBackgroundTimer(() => this.updateValue(), 1000);
        } else {
            await BackgroundTimer.start();
            timer = BackgroundTimer.setInterval(() => {
                this.updateValue()
            }, 1000);
        }
    }

For stopping timer must do like this.

resetTimer = async () => {
        this.setState({ timerValue: 0, timerDisplayValue: '' });
        if (isAndroid) {
            await BackgroundTimer.stopBackgroundTimer();
        }
        if (timer) {
            await BackgroundTimer.clearInterval(timer)
        }
        await BackgroundTimer.stop()
    }

zymr-viveksharma avatar May 31 '19 08:05 zymr-viveksharma

i've found some solution, but i'm not sure how much is it correct

modify index.js as 1)

this.uniqueId = 0
this.callbacks = {}
this.bool = true     <--------- ADD THIS ONE 
backgroundClockMethod(callback, delay) {
    this.backgroundTimer = this.setTimeout(() => {
        if (this.bool) { <--------- ADD
	    callback()
	    this.backgroundClockMethod(callback, delay)
	} <--------- ADD
    }, delay)
}
stopBackgroundTimer() {
    this.bool = false  <--------- AND THIS LAST
    this.stop()
    this.clearTimeout(this.backgroundTimer)

}

and use for IOS and ANDROID

    BackgroundTimer.runBackgroundTimer(() => { //CODE HERE }, delay) 
    BackgroundTimer.stopBackgroundTimer() 

chiefchief avatar May 31 '19 09:05 chiefchief

@davidcunha try this solution from stackoverflow, worked for me on android and ios https://stackoverflow.com/questions/54833140/react-native-background-timer-never-stops/56473096#56473096

vemarav avatar Jun 06 '19 07:06 vemarav

@davidcunha try this solution from stackoverflow, worked for me on android and ios https://stackoverflow.com/questions/54833140/react-native-background-timer-never-stops/56473096#56473096

If i set a timer of 60 seconds and i background the app in ios the app gets terminated after 30 seconds.

julianklumpers avatar Oct 26 '19 18:10 julianklumpers

It seems like we are experiencing this too now. Are others too?

jaltin avatar Sep 21 '21 12:09 jaltin

Does anyone have an idea what is the source of the issue?

andrei-tofan avatar Sep 22 '21 11:09 andrei-tofan