Relatively new to obj-c and iOS but I've created a little app that is built around 4 simple, stopwatch-style timers. The user starts up a timer, it starts counting and they go on with their life. When they open up the app they can see how long it's been going. Individually, these timers would be identical to the one in the Apple Clock app.
This works "most" of the time. I've had timers running for days, started, stopped, reset, started up again. What I've noticed though is that if the app is pushed just a little too far down the multitasking drawer, the next time I open up the app all my timers will be at zero, and the app will be in it's freshly launched state.
To me this appears related to the OS thinking my app is not needed, killing its threads/processes/whatever to free up memory. For the apps intended audience, it will be a frequently checked app so this may not crop up as a problem, but it seems that there should be some technical approach to ensure that my stopwatches never fail. I'm just not sure where to look for this kind of functionality. Any thoughts appreciated.
Thanks!
You are doing it wrong.
If you build a stopwatch app with 10 timers you need exactly one NSTimer.
And this timer refreshes the display. This timer is not needed when the app is in background.
NSTimers have the problem that they can be late, and they should not be used to schedule time critical stuff (like counting a second up).
Store the current NSDate when you start a timer and display the difference in the app.
Store those NSDates to NSUserDefaults and they will even survive a restart of the device.
Try this approach: For your "timers" keep track of the start time only, and for display purposes calculate the number of seconds since the start time and display accordingly.
Store these start times so that even if the app is "evicted", you'll still know when a timer was started and can calculate how much time has elapsed.
The only reason to use an NSTimer would be if you want to show the timer's seconds ticking by.
Also have a look at the delegate methods applicationDidEnterBackground and applicationWillEnterForeground. In these methods you'd want to invalidate and re-establish an NSTimer (if you're using one), respectively.
There is a lot of Apple documentation about what you can and cannot do in the background here.
Unfortunately there is no way to ensure that your app will continue to run in the background the way you want it to.
You can use -beginBackgroundTaskWithExpirationHandler: to request up to ten minutes of additional execution time but in all likelihood your application will be terminated.
I've heard an Apple engineer refer to this as 'evicting'.
Related
I need to play several sounds, and if one sound is already playing, the subsequent sounds will be queued.
I'm using MCI API calls.
Until now, I'm using this sound player as a COM control with InterOp.
However, the COM control causes some COM Release errors, so I would like to solve it differently.
I would like to know if I should solve this by a usual NET class in my form using a background task so that I'm completely indepenent on how heavy the load of my application is so that finished sounds can immediately trigger playing the next sound without any delay, or if that would be an overkill, and background tasks should only be used on long, blocking operations.
My sound player basically only uses a timer to check with an API call if the MCI has stopped playing and then starts the next sound.
Also, it provides events when a sound was started, stopped or errored-out.
Or if I should encapsulate the player in a separate NET project and reference it? Then I would also be independent on the workload of the main application.
Seamless playing of the sound queue would be essential to me.
I know you can use the AudioQueueGetCurrentTime method to get the current time. Is there a callback or something that continuously gets called to update your timeline? Or do you just have to create a timer to continuously call AudioQueueGetCurrentTime?
I don't remember any other way than timer, but I guess it is possible to implement periodic notifications without timer since you manually enqueue buffers and you know bitrate, etc...
As a side note: why not give a try to AVFoundation (available since iOS 4), it's all implemented there and until you have standard formats playback or you have to support iOS 3.x you don't really have to deal with AudioQueue horror.
I have an application used mainly with uiwebview since we have a lot of work based on the website so it is quicker to show it directly on webview without re-coding it. The problem with webview, it is very expensive on memory. The UI is running fine throughout 10 times of going through from beginning of the application to the end of the application and repeating the process. On the other hand, the webview started to get slow when it doing some javascript animation using Canvas object. I have put in some code to remove NSUrlCache when it received warning.
Our application is based on navigating through stacked pages. When it gets to the end, user basically goes back to beginning. In my mind, I wanted to relaunch the application when I knows the application started to run slow. I know it is not a good idea to do this but I don't know what is the best way of reclaim the memory. I have looked through all of my code and have released what i have to released. The Application is going well without problem but it is just the uiwebview caused the performance.
Please Help...
Short answer: Not possible. You'll have to find a different way to address your performance problems.
If UIWebView is bottlenecking your application then the solution would be to NOT use UIWebView. You cannot simply "restart" or "reset" your application. If performance is decreasing over time as your app is being used then this suggests that you might not be managing your memory or object allocations properly. You can use the Leaks instrument to debug your application and try to hunt down memory leaks and you can use the Allocations instrument to analyze your object allocations.
I have an iPhone app that, for some users, sometimes behaves as if with the main UIView has been removed from the view hierarchy. It always happens coincident with a significant event in the game. Other Core Graphics-drawn UIViews that are above it in the z-order remain, but the main one (an OpenGL view) appears to be gone, leaving the background (a solid color).
The app does not crash (it keeps running, without the view), and this seems to happen very consistently for affected users. Unfortunately I am not able to reproduce it.
I suspect a memory issue -- that would be the easiest explanation -- but based on my reading it looks like didReceiveMemoryWarning only deallocs views that aren't visible, and aside from that the memory usage for my app is pretty small. Also, the "significant event" only results in OpenGL drawing and a SoundEngine call -- no view manipulation.
Anybody out there seen something like this before?
Yes, infact one of my applications very occasionally exhibits this problem and it does seem to be memory related.
I have had no success tracking it down either by debugging or analyzing the program flow. I have verified that the view in question is destroyed and not just hidden in some way.
It happens so infrequently that I haven't looked into it to deeply, but I do think it's caused by something in the OS in some way,
You can easily test low memory in the simulator to debug this problem if it is memory related.
The problem ended up being an uncaught NSException (from a third party library) thrown in the app's timer thread, which killed the timer thread but not the rest of the app. The good news is that crash reports are generated in this case, which can make tracking it down much easier if you know to look/ask for them.
As is made clear in the SDK documentation, when your app is running low on memory, views that are not in use can be collected. When it's needed again, it's re-created. This is to conserve precious iPhone resources. Your best bet is to retain the view so it can't be released.
I have a college assignment due quite soon, and I need to be able to call a C++ dll that takes a long time (possibly infinte, it relies on user input)to execute. Im calling this through VB. My VB GUI freezes up when this happens, and I would like to keep the GUI responsive, so that the user can stop this possibly infinte loop.
Can anyone suggest the best/fastest way of doing this?
A bit of background, the C++ is trying to keep score on a snooker table using a webcam, and while the VB scoreboard updates easily, I would like to script it so that the analysis is almost continuous, while still allowing the user to interact. Currently the project requires the user to press a button to start the shot analysis, but it would be preferable if the program scripted itself. I have only realised this problem now and the deadline is very soon.
Update: Our lecturer suggested an option to solve the problem, but it would appear that most options here and the one he suggested will not work for us as the processing time required for the webcam image capture is too great to handle due to hardware constraints. Thanks for taking the time to help, it was much appreciated!
The best way to handle threading in VB.NET is via the System.Threading namespace.
You might also look into Application.DoEvents()
Try the system.Threading as Mark said, also try looking at Background Worker Process which is a bit simpler in VB.NET. Readup here
I would definitely use the Background Worker process. You can drag it onto your form and use the DoWork sub routine to actually do the work that is freezing your GUI thread. You can also use the ReportProgress event to actually provide progress back to your form.
As for your question regarding two separate threads, If both steps take a long time to complete I would run them in the same thread one after the other.
The one thing that could bite you with using the thread is cross-threading. In the context of your problem this means not having your second thread update form controls.
A really good resource for how to implement this code along with dealing with cross-threading is this PDF.
I also should point out that if you are using .net 1.0/1.1 you can still do the multi-threading, but don't have the luxary of having a background worker control. You'd just have to create a new thread from the System.Threading Namespace.
Just as an alternative, you could have your C++ actually processing in the background all the time. When called from VB, it would just be retrieving data from it or sending it a command (start, quit, ???) all of which would return instantly.
This could also give you more reliability since C++ would never miss video frames while VB was collecting the garbage or doing the dishes or whatever VB does in the background--C++ is going to let you be closer to a real time system.
RE: the comment about how.
What I'd probably do is have my VB programs send "Messages" to the C++ (As I said). A message is just a way to think of a function/method call--but generally they return quickly.
The "Start" message would tell the C++ code to start it's thread running and return. This is a Linux C++ thread howto, I'm not sure if you need to do something different in windows (I'd hope not, but I haven't used C++ as my main dev. language in decades).
If that doesn't work, just google "C++ Threads"
Sending a "Stop" message would stop the thread (and probably free resources).
A "Get Data" call would go to the location that the C++ thread used to store data, grab it and return.
Sorry to be so general, I'm pretty heavily Java these days.
I'm surprised nobody has suggested using a BackgroundWorker yet.