OS X 10.9 & App Nap

Mac OSX specific forum
User avatar
J. Baker
Addict
Addict
Posts: 2196
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

OS X 10.9 & App Nap

Post by J. Baker »

Ok, so I received two reviews for my app "Joi" not functioning after a short amount of time. Then I saw one of the users got a refund from my iTunes Connect account. So I looked into what was going on and found the "App Nap" feature (OS X 10.9 or better) on MacBook's were effecting my app.

Apple states that "App Nap" doesn't take effect unless the app window goes into the background and is no longer processing anything. This isn't the case. My app is always processing as that is how it works but for some reason my app was taking an "App Nap".

If your app works in the background and you're getting bad feedback or just couldn't figure out why your app just quits working, make sure to enable "Prevent App Nap". This can be done by right clicking on the app and selecting "Get Info". There you'll see the "Prevent App Nap" option.

Cheers! ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: OS X 10.9 & App Nap

Post by PB »

> my app "Joi"

Got a link in the App Store? I can't find it. :(
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
J. Baker
Addict
Addict
Posts: 2196
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: OS X 10.9 & App Nap

Post by J. Baker »

PB wrote:> my app "Joi"

Got a link in the App Store? I can't find it. :(
Sure, here you go...
https://itunes.apple.com/us/app/joi/id511655466?mt=12
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
J. Baker
Addict
Addict
Posts: 2196
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: OS X 10.9 & App Nap

Post by J. Baker »

Here's how you can enable or disable "App Nap" within your app. The "CFBundleIdentifier" has to be within your plist. As you can see mine is "com.posemotion.joi" for my Joi app. If you're not a Mac App Developer I'm not sure what you would do.

Code: Select all

RunProgram("defaults", "write com.posemotion.joi NSAppSleepDisabled -bool yes", "")
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: OS X 10.9 & App Nap

Post by Danilo »

Interesting topic, J. Baker. Thank you!

- Having trouble programatically disabling AppNap for Java application
The correct way to disable App Nap is to use the

Code: Select all

-[NSProcessInfo beginActivityWithOptions:reason:]
and

Code: Select all

-[NSProcessInfo endActivity]
APIs within your app ...
and only when your app is actually doing work.
- Disable AppNap for all apps: (not recommended if you want to save energy and the world)

Code: Select all

defaults write NSGlobalDomain NSAppSleepDisabled -bool YES
or

Code: Select all

sudo defaults write NSGlobalDomain NSAppSleepDisabled -bool YES
Slow Finder and sleepy Mail?

Code: Select all

sudo defaults write com.apple.Finder NSAppSleepDisabled -bool YES
sudo defaults write com.apple.Mail NSAppSleepDisabled -bool YES
User avatar
J. Baker
Addict
Addict
Posts: 2196
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: OS X 10.9 & App Nap

Post by J. Baker »

Thanks Danilo! :D

I'll give that a try as my method didn't work. I thought it did at first but after testing, it was a no go. :?
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: OS X 10.9 & App Nap

Post by Danilo »

J. Baker wrote:I thought it did at first but after testing, it was a no go. :?
Is it because you don't have admin privileges? Most examples use sudo to get admin privileges,
so it may only work in admin mode.

With NSProcessInfo:

Code: Select all

;
; NSActivityOptions is a 64bit typedef - use it with quads (.q) !!!
;
#NSActivityIdleDisplaySleepDisabled             = 1 << 40
#NSActivityIdleSystemSleepDisabled              = 1 << 20
#NSActivitySuddenTerminationDisabled            = (1 << 14)
#NSActivityAutomaticTerminationDisabled         = (1 << 15)
#NSActivityUserInitiated                        = ($00FFFFFF | #NSActivityIdleSystemSleepDisabled)
#NSActivityUserInitiatedAllowingIdleSystemSleep = (#NSActivityUserInitiated & ~#NSActivityIdleSystemSleepDisabled)
#NSActivityBackground                           = $000000FF
#NSActivityLatencyCritical                      = $FF00000000


Procedure BeginWork(option.q,debugReason.s="Starting Activity")
    Protected NSProcessInfo = CocoaMessage(0,0,"NSProcessInfo processInfo")
    If NSProcessInfo
        ProcedureReturn CocoaMessage(0,NSProcessInfo,"beginActivityWithOptions:@",@option,"reason:$",@debugReason)
    EndIf
EndProcedure

Procedure EndWork(activity)
    Protected NSProcessInfo = CocoaMessage(0,0,"NSProcessInfo processInfo")
    If NSProcessInfo
        CocoaMessage(0,NSProcessInfo,"endActivity:",activity)
    EndIf
EndProcedure



work = BeginWork(#NSActivityUserInitiated)
If work
    Debug work
    ; do work
    EndWork(work)
EndIf
If your 'Joi' app is a background process, maybe #NSActivityBackground is enough. On the other side,
it is a user initiated activity to translate joystick co-ordinates to keys. Don't know if they check this when updating
your program in AppStore.
User avatar
J. Baker
Addict
Addict
Posts: 2196
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: OS X 10.9 & App Nap

Post by J. Baker »

Thanks Danilo for the code. I had been messing with this all last night and again today. Your example works fine. Even when you add a loop and other stuff. For some reason when I add it to my app it doesn't work. No matter what option combinations I use. I'll keep fiddling with it and hope I can get it working soon. Thanks again! ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
J. Baker
Addict
Addict
Posts: 2196
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: OS X 10.9 & App Nap

Post by J. Baker »

Sweet! I got pass the app nap crap without any of the needed code.

Here's how app nap works. If you are processing very little (defined by some OS X programmer at Apple) app nap will kick in, even if your app is still working/processing. But if you use a While Wend and keep processing what needs to be done until finished, app nap doesn't kick in and disable your app. Just make sure to put a Delay() in your loop.

So, it seems large loops don't process enough information for Apple in a set amount of time, whatever that is.
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
Niffo
Enthusiast
Enthusiast
Posts: 504
Joined: Tue Jan 31, 2006 9:43 am
Location: France

Re: OS X 10.9 & App Nap

Post by Niffo »

Good to know : the "beginActivityWithOptions:reason:" method of the NSProcessInfo object has to be called inside each thread if you make use of thread(s).
Niffo
spacebuddy
Enthusiast
Enthusiast
Posts: 364
Joined: Thu Jul 02, 2009 5:42 am

Re: OS X 10.9 & App Nap

Post by spacebuddy »

J. Baker,

Will Apple allow RunProgram("defaults", "write com.posemotion.joi NSAppSleepDisabled -bool yes", "") in your program if you submit to the App Store???


Also, my PureBasic App has no "Prevent App Nap" checkbox when you check it with Get Info. :shock:

I also tried this in the Applications Plist file but it did not work
<key>NSAppSleepDisabled</key>
<true/>
User avatar
J. Baker
Addict
Addict
Posts: 2196
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: OS X 10.9 & App Nap

Post by J. Baker »

spacebuddy wrote:J. Baker,

Will Apple allow RunProgram("defaults", "write com.posemotion.joi NSAppSleepDisabled -bool yes", "") in your program if you submit to the App Store???


Also, my PureBasic App has no "Prevent App Nap" checkbox when you check it with Get Info. :shock:

I also tried this in the Applications Plist file but it did not work
<key>NSAppSleepDisabled</key>
<true/>
I'm not sure if they would or not. Wouldn't hurt to submit it and try.

That's odd. I don't understand why it would not.

I just added a one time popup on my app stating to disable app nap as I couldn't find a solution that worked either. I thought I had it solved once but sure enough, the app went to sleep. I think it's one of Apple's stupidest features. If an app isn't doing anything, it shouldn't be using much power anyway. I've read complaints on many forums complaining about this. Just because an app is using little resources while running doesn't mean it's not doing anything. But then again, Apple is known for large bulky apps. I find it annoying as there are still people who like to create small, fast and minimal resource using apps.

I actually call app nap the "dictator" feature of OS X. Apple should not control what apps it thinks should be running or not.

Oh and Apple has created v2 of their plist recently. You may want to look that up before submitting your app. ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
spacebuddy
Enthusiast
Enthusiast
Posts: 364
Joined: Thu Jul 02, 2009 5:42 am

Re: OS X 10.9 & App Nap

Post by spacebuddy »

yeah, this app nap thing is crazy. My application does not work well once it goes into app nap. :cry:

Will try to find a permanent solution and post it here :lol:
Niffo
Enthusiast
Enthusiast
Posts: 504
Joined: Tue Jan 31, 2006 9:43 am
Location: France

Re: OS X 10.9 & App Nap

Post by Niffo »

The solution posted by Danilo worked well for me. Thank you again Danilo !
Niffo
spacebuddy
Enthusiast
Enthusiast
Posts: 364
Joined: Thu Jul 02, 2009 5:42 am

Re: OS X 10.9 & App Nap

Post by spacebuddy »

I put beginActivityWithOptions:reason:" in my Thread and it worked. App Nap has stopped.

App Nap is like virus, but now it is stopped. :D :D :D
Post Reply