最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

expo - React Native Background timer is killed by OS when backgrounded - Stack Overflow

programmeradmin3浏览0评论

I'm facing an issue with BackgroundTimer. When running the app on the development server (npx expo run:android), it continues working in the background for several minutes without issues. However, after building the APK and installing it, the app freezes after about 40 seconds in the background and sometimes even gets killed if left in the background for too long. Why is it behaving differently in the development environment compared to the production build?

I tried older react-native-background-timer and also newer @boterop/react-native-background-timer. These two packages had a same issue.

const stopTimer = () => {
    if (Platform.OS === 'ios') {
        clearInterval(timer)
        BackgroundTimer.stop();
    } else {
        BackgroundTimer.clearInterval(timer);
    }
};

useEffect(() => {

        if (isTimerRunning && !isPaused) {
            if (Platform.OS === 'ios') {
                BackgroundTimer.start();
                timer = setInterval(() => {
                    setTimerSeconds(prevTime => prevTime - 1);
                    checkTime()
                }, 1000);

            } else {
                timer = BackgroundTimer.setInterval(() => {
                    setTimerSeconds(prevTime => prevTime - 1);
                    checkTime()
                }, 1000);
            }
      }
    
    return () => {
        if (Platform.OS === 'ios') {
            clearInterval(timer);
            BackgroundTimer.stop()
        } else {
            BackgroundTimer.clearInterval(timer)
        }

    };


}, [isTimerRunning, isPaused, timerSeconds, setTimerSeconds]);
发布评论

评论列表(0)

  1. 暂无评论