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

IOS App should not be killed in background state

Open jipEbizz opened this issue 4 years ago • 11 comments

In android background timer is work perfect but in IOS when app is in background state since 30 seconds then app is killed in background state.

Please help me to run background timer in IOS for more than 30 seconds or some minutes. Thanks.

jipEbizz avatar Oct 14 '20 12:10 jipEbizz

I am also having the same issue. I am using background timer's setInterval method. I tried to add background modes in IOS, but still app automatically stop timer after 30 sec and then resume it when we press back app. I also tried to add UIBackgroundModes in info.plist file still same.

Does anybody found any work around for this issue

aniket-bhange avatar Dec 07 '20 09:12 aniket-bhange

I am also having the same issue. I am using background timer's setInterval method. I tried to add background modes in IOS, but still app automatically stop timer after 30 sec and then resume it when we press back app. I also tried to add UIBackgroundModes in info.plist file still same.

Does anybody found any work around for this issue

Facing same problem timer stop after 30 sec

Prithhiraj avatar Jan 10 '21 18:01 Prithhiraj

any solution

yasir-syed avatar Mar 12 '21 16:03 yasir-syed

Thats's the default behaviour for ios, you can't run app in background for more than 30 seconds.

ammarahm-ed avatar May 24 '21 05:05 ammarahm-ed

This is causing me problems as well. The main issue that I understand is that the expiryHandler in this BackgroundTimer is not allowing IOS to terminate the backgroundtimer correctly. So this means that when you send your app to the background and start this backgroundTimer, after about 30 seconds, Ios tries to suspend your app gracefully by killing all background tasks. The problem is, it's unable to truly stop the background task of the background timer. So after a 35-40+ seconds, the IPhone has no choice and the ios watchdog kicks in to terminate the app. This makes it so that when you launch your app once again (from the background) your app has to reload and the current state of the app is lost.

A few things I have observed is that when you play a sound, this restarts the 30 second countdown of the app being suspended (on the main thread), but the ios watchdog is individually watching each background task so any other threaded task aka the backgroundtimer task is gonna get your app killed, and javascript doesn't really run on the main thread while the app is in the background as far as I know

ucheNkadiCode avatar May 16 '22 06:05 ucheNkadiCode

The UIApplicationEndBackgroundTaskError is coming from this block of code in RNBackgroundTimer.m

RCT_EXPORT_METHOD(setTimeout:(int)timeoutId
                     timeout:(double)timeout
                  resolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
    __block UIBackgroundTaskIdentifier task = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"RNBackgroundTimer" expirationHandler:^{
        [[UIApplication sharedApplication] endBackgroundTask:task];
    }];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, timeout * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
        if ([self bridge] != nil) {
            [self sendEventWithName:@"backgroundTimer.timeout" body:[NSNumber numberWithInt:timeoutId]];
        }
        [[UIApplication sharedApplication] endBackgroundTask:task]; //this is where the error is
    });
    resolve([NSNumber numberWithBool:YES]);
}

Anyone have any pointers to what could be causing this?

ucheNkadiCode avatar May 16 '22 06:05 ucheNkadiCode

any update?

thanhnn-bap-jp avatar Jul 28 '22 04:07 thanhnn-bap-jp

same.

Simoon-F avatar Aug 06 '22 00:08 Simoon-F

same

Aaronphy avatar Jan 11 '23 14:01 Aaronphy

The UIApplicationEndBackgroundTaskError is coming from this block of code in RNBackgroundTimer.m

RCT_EXPORT_METHOD(setTimeout:(int)timeoutId
                     timeout:(double)timeout
                  resolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
    __block UIBackgroundTaskIdentifier task = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"RNBackgroundTimer" expirationHandler:^{
        [[UIApplication sharedApplication] endBackgroundTask:task];
    }];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, timeout * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
        if ([self bridge] != nil) {
            [self sendEventWithName:@"backgroundTimer.timeout" body:[NSNumber numberWithInt:timeoutId]];
        }
        [[UIApplication sharedApplication] endBackgroundTask:task]; //this is where the error is
    });
    resolve([NSNumber numberWithBool:YES]);
}

Anyone have any pointers to what could be causing this?

any update?

Aaronphy avatar Jan 11 '23 14:01 Aaronphy