Make OS X Swift application run in the background - xcode

I'm making a Clock app in swift for OS X. I have a Timer in my Clock app that works kinda like the one in iOS. My problem is whenever I put my app into the background or minimize it the application kinda freezes or stops counting after being in the background for a minute or so. It starts working right where it left off when I bring it into the foreground. Is there anyway I can make my application run in the background? I've looked and haven't found anything that works.
Thanks to anyone who can help.

Are you relying on the timer to "count" the seconds? You should only use the timer to provoke an update of the display. When the timer fires, you should check the actual time and calculate what should be displayed based on that time, not how many times your timer has fired. It should never be the case that your app "starts working right where it left off" (if, by that, you mean that it shows the same time as when it was backgrounded and starts counting up from there again).
What's probably happening is that your app is being put into App Nap. Also see the Energy Efficiency Guide for Mac Apps.

Related

Getting memory leaks from Appcelerator Alloy app

I have an Alloy app. It has got 7 windows and opens at same time. When user close a opened window $.removeListener(); $.destroy(); codes runs at window close event. But I am getting memory leaks on Android device. %90 windows has got ListView, every window has got max 2 Listview. What is the right approach for multiple windows?
First of all, why you would want to open 7 windows at same time when user can only see max 1 window at a time.
It's dead simple, open only that window which user should see first, & create a link-flow to other window in previous window.
Can you think of any app on Play Store which does so, if you have, then please send me its link, I would really love to review it?
But if you mean to say that user will see all windows at same time in a scrolling behaviour or like paging, then go to Ti.UI.TabGroup
Are you 100% sure that your event listeners are being removed?
I don't know the function $.removeListener(); is this a custom function?
As a general rule I try and put as many of my event listeners into the xml, as these are automatically removed, and have a custom function destroyMe() that runs onClose which removes any other listeners that I may have used and $.destroy()
Ti.App.addEventListener is a killer too, make sure these are removed if you use them!
ps: i totally understand the 7 windows bit :-)

code that's always executed before app gets visible?

I would like to secure an app of mine with a lockscreen. Currently, it's shown in didBecomeActive, but it seems as if that's a little bit too late as the content is visible for a short time sometimes before the lockscreen is shown.
As the lockscreen is not always shown (e.g. only if the app was suspended for more that 30 seconds), I don't want to add it in applicationWillResignActive.
One method would be to start a timer then and add the lockscreen after 30 seconds in the background, but I don't like that solution, I would prefer a method like WILLbecomeActive- is there a way to achieve that?

OS X Cocoa timer not fired when application on background

A third-party library provides a function I need to call every 100ms. Setting up a timer to do that works very well as long as my app is on foreground. When my app is on background timer works for a while but after a about a minute timer is called only after 10 second delay. The same happened when I created a separate thread with usleep-function. Is there any way I can keep timer running while my app is on background?
Use beginActivityWithOptions:reason: to disable app nap for your application.
You should try and re-architect to avoid these sorts of frequent timers, especially when your application is in the background. Making this sort of change will improve battery life for your users.

Cocoa: App uses more CPU when in the background?

I'm making a mac app that appears as a popover from the menu bar. The view has several components, when everything is running and the popover is open I see that it is taking up about 3% CPU. However when I minimize the popover and let it run it the background it jumps up to 6-7% CPU. This does not make any sense to me since the view is no longer being shown so I would imagine less would be required to run.
However I am not doing anything when I close the popover, just sending a [popover close] message.
Is there something else I should be doing when I close the popover to keep down CPU usage?
Thanks
Without knowing your code, it is hard to say what's going on. You will need to find that out by using Instruments Time Profiler. Once you know what the application is doing while minimized, you should be able to locate and fix the issue.

background cocoa app/utility that displays full screen image at given time

I want to write an app for Mac OS X. The app/utility would act according to preset schedule. I will have different time intervals at which I want this app to show a certain image in full screen regarding if there are other apps running at the time.
The real question is how to check this time interval in the background and bring this app in-front and enter full screen. I know how to go full screen, but I am stuck at bringing this app in-front of all other apps.
To schedule a method to be called after an interval, just use NSTimer and one of its +scheduledTimer... methods.
To force your application to be active, call [NSApp activateIgnoringOtherApps:YES].
If you want your window to appear above absolutely everything, including the screensaver, set its level to NSScreenSaverWindowLevel + 1.
The only solution that comes immediately to mind is use AppleScript, e.g. if you execute the following AppleScript from within your app:
tell application "MyBackgroundApp"
activate
end tell
There is an Apple Tech Note with sample code for sending AppleScript from a Cocoa app using NSAppleScript.
How to force Mac window to foreground?

Resources