19

I am writing an application that receives OSC messages. However, due to 10.9's App-Nap-technology the application stops reacting on these messages after leaving foreground. I want to disable AppNap for my application running on 10.9, but still be able to run on 10.8, so I tried this piece of code, but it does not show any effect.

if ([[NSProcessInfo processInfo] respondsToSelector:@selector(beginActivityWithOptions:reason:)]) {
    [[NSProcessInfo processInfo] beginActivityWithOptions:0x00FFFFFF reason:@"receiving OSC messages"];
}

See full code on github.

The if-condition seems to work as expected and is executed on 10.9-machines. But nevertheless the app is sent to sleep. (Activity Monitor shows "App Nap: Yes" for my application).

Thanks for your help!

danielbuechele
  • 3,104
  • 2
  • 18
  • 29
  • have you tried `NSActivityIdleSystemSleepDisabled| NSActivitySuddenTerminationDisabled` for your activity options? – Brad Allred Nov 07 '13 at 22:07
  • As my app should also build again 10.8 I can't use the named values but used the hex-values. I tried `beginActivityWithOptions:(1ULL << 20)|(1ULL << 14)` but it didn't work. – danielbuechele Nov 07 '13 at 23:24
  • If you use build for 10.9 and 10.8 you should choose the SDK to be 10.9 so you will get all the names and don't have to use hex values. But you put the deployment target to 10.8. This means all 10.9 specific stuff which might not be in 10.8 you have to verify first if its there (as you did above with respondsToSelector: for example) – Andreas Fink Nov 08 '13 at 09:01
  • Alright. Did that. However, `beginActivityWithOptions:NSActivityIdleSystemSleepDisabled|NSActivitySuddenTerminationDisabled` does not prevent App Nap. – danielbuechele Nov 08 '13 at 20:00
  • @danielbuechele did you find a way to disable app nap? I'm also fighting it. – Tiago Jan 06 '17 at 17:25

2 Answers2

12

the activity need to be stored in a property. So I added this in the header-file:

@property (strong) id activity;

and then used this implementation.

if ([[NSProcessInfo processInfo] respondsToSelector:@selector(beginActivityWithOptions:reason:)]) {
    self.activity = [[NSProcessInfo processInfo] beginActivityWithOptions:0x00FFFFFF reason:@"receiving OSC messages"];
}

Thanks to all contributors!

danielbuechele
  • 3,104
  • 2
  • 18
  • 29
5

From your description of the problem it sounds like you might want to take a look at WWDC 2013 704 and review the section (near the end) on background continuous work. A look at the manual for the setpriority command may also be helpful. I haven't had a need to disable app nap but I think manual change of the process priority is a good approach.

2013 WWDC Video 205 (near the middle) describes some interesting aspects of app nap that can affect the outcome of whether the app is throttled. Video 209 from WWDC 2013 presents the following screenshots on Occlusion (the point where app nap engages) may be useful.

App Occlusion

Window Occlusion

Occlusion Example Hope this helps.

Tommie C.
  • 12,895
  • 5
  • 82
  • 100