12

I have a background application which requires to send a keep-alive to another process every 1.5 seconds. Everything works smoothly in OSX 10.7 and 10.8, but under OSX 10.9 many keep-alive notifications are missed, sometime up to 3. Usually everything works fine for the first 3 or 4 minutes then problems start to occur.

After further inspection, it seems that OSX Mavericks "Timer Coalescing" feature would be responsible for making the decision of extending the requested 1.5 seconds up to 4.0 seconds.

Is there any way to indicate in a NSThread not to coalesce? Or at the very least to indicate the maximum coalescing variations allowed?

See code below for reference:

+(void)keepAliveThread
{
    @autoreleasepool {
        void (^keepAlive)() = ^ (){
            // (snipped!) do something...
        };
        dispatch_queue_t mainQueue = dispatch_get_main_queue();
        while( [NSThread currentThread].isCancelled == NO )
        {
            @autoreleasepool {
                dispatch_async(mainQueue, keepAlive);
                [NSThread sleepForTimeInterval:1.5];
            }
        }
    }
}
3
  • Pretty sure the user can turn it off per-app, though that's probably not the solution you really want. – Kevin Oct 24 '13 at 23:08
  • 1
    Why does the keep alive work need to be done on the main thread? – trojanfoe Oct 25 '13 at 9:12
  • 1
    It does not, the problem was encountered regardless if the work actually happens on the main thread or secondary thread. App Nap kicks in for all applications that do not have a visible UI element currently active. If all the UI elements are hidden or closed, the application will see its timers slowed down by App Nap. – ekscrypto Oct 25 '13 at 19:35
8

A user on the Apple Developer forums actually recommended I watch a video from WWDC 2013 entitled "Improving Power Efficiency with App Nap"; in which I found the solution:

static dispatch_source_t _keepAliveTimer;

+(void)enable
{
    _keepAliveTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, DISPATCH_TIMER_STRICT, dispatch_get_main_queue());
    dispatch_source_set_event_handler(_keepAliveTimer, ^{
        // do something
    });
    dispatch_source_set_timer(_keepAliveTimer, dispatch_time(DISPATCH_TIME_NOW, 1.5 * NSEC_PER_SEC), 1.5 * NSEC_PER_SEC, 0.5 * NSEC_PER_SEC);
    dispatch_resume(_keepAliveTimer);
}

This piece of code will fire a timer at 1.5 seconds (give or take 0.5 seconds) regardless of the LSUIElement state and will prevent App Nap from kicking in for that timer only.

6

It sounds like you're running the background application without setting an appropriate .plist key.

If you are using a background application, then you must either set the 'Application is agent (UIElement)' (LSUIElement) option to YES or the 'Application is background only' (LSBackgroundOnly) option to yes in the application's plist, otherwise it will be subject to App Nap, which is what you're experiencing in this case. I would not expect timer coalescing to produce the huge gaps in the timer intervals.

The LSUIElement is intended for apps that may just have a floating window or a status-bar item. They don't get a menu bar, and they don't get a dock icon.

App Nap is designed to affect up-front user applications. There are, according to the docs there are 4 things that will cause the app to be sent into app-nap:

  • It is not visible—if all of an app’s windows are either hidden by other windows or minimized in a hidden dock, and the app is not in the foreground
  • It is not audible
  • It has not explicitly disabled automatic termination
  • It has not taken any power management assertions

If you want to prevent a user application from experiencing App nap, then you will have to follow one of the supported mechanisms for causing one of these states to not be active.

If you use the IOPmlib.h API, you can create a power management assertion for your app which will prevent app nap.

Alternativaly, you can disable automatic termination using:

[[NSProcessInfo processInfo] disableAutomaticTermination:@"Good Reason"];

And to enable automatic termination again:

[[NSProcessInfo processInfo] enableAutomaticTermination:@"Good Reason"];

But this is generally intended for code that needs to be done before an app is considered 'good to stop' e.g. writing out preferences.

Apple do state somewhere in the docs that if your app is experiencing issues related to app nap that you should file a radar so that it can be determined if it's a bug in their implementation.

6
  • I was thinking that a NSThread with a fixed sleepInterval would have had more stringent and fixed behaviour than a NSTimer; hence why I did it that way. – ekscrypto Oct 25 '13 at 3:07
  • Tried using a NSTimer, after 30 minutes of running in background the NSTimer interval, set to 1.5seconds with a 1.0 seconds allowance, did not get fired for over 12 seconds. – ekscrypto Oct 25 '13 at 4:03
  • From the question, it sounds like this is either an LSUIElement or LSBackground only app, which aren't subject to App Nap at all. – jscs Oct 25 '13 at 6:11
  • 3
    You can check if app nap is applying using the 'taskinfo' command. If it is, you can use the new NSProcessInfo APIs (not automatic termination, the new in 10.9 ones), and the new 'precise' flag for dispatch timers. Note that sleep() is subject to app nap exactly the same way NSTimer and every other timer-like thing is. – Catfish_Man Oct 25 '13 at 6:31
  • The application from which I'm doing this is actually switching between LSUIElement YES and NO depending on whether UI elements (a control panel) needs to be displayed. – ekscrypto Oct 25 '13 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.