pausing execution for 1 second - iphone

I have an IBAction which starts a number of timers.
I would like to have a second or a pause for a certain time. How can I pause execution?
I know of the [self performSelector:#selector(someMEthod:) withObject:someObject afterDelay:1.0];
but how can I just cause a delay without calling anything?
Thanks

You can call sleep() or +[NSThread sleepForTimeInterval:] but please don't do that on the main thread. Pausing execution of the main thread means your app's UI will be blocked for that time and appear to the user as if it had crashed. If you blocked the main thread for more than a few seconds, Apple's watchdog timer would kill your app instantly.
See this blog post by Jeff LaMarche for more on this issue: http://iphonedevelopment.blogspot.com/2010/05/psa-respect-main-thread.html

You don't want to cause a delay without calling anything. It's bad for the UI, and does not fit Cocoa Touch's event driven paradigm.
Cut the routine in which you want to pause into (at least) two parts/halves. Have the first half set up a delayed call to the second half (perform selector with delay, timer, queue, etc.) and then return to the main loop. The OS will call the second part, later, after potentially doing useful stuff in the mean time (giving the user a responsive UI or saving battery life, catching up with background email, etc.)
You may have to learn how to save state between parts (loop variables, etc.), something that some new programmers seem to miss in their learning.

You can use sleep(int_No_of_Sec); // eg. sleep(1);

Related

UIActivityIndicator issues -- sometimes it doesn't stop

I have an activity indicator that is working fine. Most of the time. But sometimes it just keeps going. The code is pretty spread about, so I'll have to explain.
I'm doing a calculation. Sometimes it only needs updating, so it's fast. Sometimes it's lengthy, and may have to get new data from a server. When the calculation takes some time, the activity indicator works. When it's fast, though, the activity indicator will start, but not stop. Is there a minimum time for the animation? Has anyone else run into this?
I'll try writing something to gate the startAnimation based on the last time the calculation was done. Any suggestions?
UPDATE: OK. it' solved. Logging (as suggested) did show up that there was second call to startAnimating, leading to a race condition: sometimes the calculation was long enough that both calls arrived before the stop. I've now eliminated the second call and it's working!!
Are you sure that you call stopAnimating from the main thread? Changes in the UI have to be made on the main thread. Otherwise you will experience nothing at all, strange behavior or delays.
Put something like that NSLog before you call stopAnimating to make sure it is called at all and from the main thread.
NSLog(#"main thread? %#", [NSThread isMainThread] ? #"YES" : #"NO!");
Doing UI updates from another thread is often the reason for unexpected results, particularly for UIActivityIndicatorViews because they are usually used with threads.
My guess is that you call endAnimation before startAnimation or not at all. These are the most common causes for such problems...
Did you try NSLog when these two methods are called?
For these situations I like to build a sort of reference counted activity indicator. Basically every time you do something on the network, you increment the network activity count. Then when it's done you decrement. If it ever hits 0, you stop it. If it's above 0, you start it.

What is a runloop?

After reading the documentation for NSRunLoop I did not understand very much. I am spawning a secondary thread that has a NSTimer in it that launch every 1sec. Which update a label on the screen with the performSelectorOnMainThread..
However to get it to work I needed a runloop but I do not understand the concept of it?
Anyone who could try to explain it?
Thanks.
A run loop is effectively:
while(... get an event ...)
... handle event ...;
It runs on a thread; the main thread has the main event loop where user events are processed and most UI drawing, etc, occurs. The documentation explains it in detail.
However, in your case, you don't need a thread.
It sounds like all you are doing is periodically updating a label in the UI; something that isn't terribly compute intensive.
Just schedule your timer in the main thread and be done with it. No need for spinning up a thread, using performSelectorOnMainThread:, or incurring all the complexities of guaranteeing data coherency across threads.
Sorry -- didn't understand your question.
Internally, a run loop works by basically putting a flag in the run loop that says "after this amount of time elapses, fire the timer". No additional threads involved and, better yet, it isn't polling to check the time. Think of a run loop as effectively maintaining a timeline. It'll passively let time elapse until there is something of interest found on the timeline (all without polling -- polling sucks. to be avoided.)
It does mean, though, that a Timer will never be 100% accurate. As well, if you have a timer repeating every second, it'll drift over time.
Also; instead of directly triggering a drawing event. Your timer should invalidate the view that needs updating, then let the underlying objects deal with when it is best to actually update the screen.
This page explains it pretty well. FTA:
A run loop is essentially an event-processing loop running on a single thread. You register potential input sources on it, pointing it to the code that it should execute whenever input is available on those sources.

Running activity indicator NOW

I want to start an activity indicator before going to disk I/O.
How do I start the indicator NOW, instead of waiting for the next display loop cycle? Or how do I force the display loop before beginning the disk I/O?
Dan
If I understand correctly your IO operation takes some time, and you want the UI to update as you're performing it, right? If that's the case, you have to move your operation to a background thread for the UI to have a chance to update itself, there's no workaround. use performSelectorInBackground:withObject: to call your IO operation, use NSOperation, or blocks if you're targeting iOS 4.0+
You can't force the indicator to start now as far as I'm aware. To push a method call so that it's the next thing in the runloop, you can use:
[self performSelector:#selector(taskToDo) withObject:nil afterDelay:0]
Which will schedule 'taskToDo' (optionally with a single argument) onto the current runloop, to occur as soon as it can. Then just let that segment of code exit. An equivalent method is to schedule an NSTimer, but syntactically that's a bit more lengthy.

Objective C - Single Background Thread

I want to run a single background thread for an iPhone application which is available in background all the time and gets executed when specific event fires and go to wait for specific event to fire to start its execution again. During the execution of thread if specific event is fired again then thread should restart its work.
I am working on a custom map application. On TouchesMoved event, I need to load the map image tiles according to the positions moved in a background thread. The problem is that when I move the map with speed the touchesMoved event is fired the previous thread has not finished its work and new thread is started. It causes thread safety issue and my application is crashed.
So I am thinking of a solution to have a single thread all the time available and starts its work when touchesMoved is fired if touchesMoved is fired again it should restart its work instead of starting a new thread. I think it will prevent the thread safety issue.
Please help
Firstly I'd echo the use of NSOperation and NSOperationQueue. You could fall-back to using NSThread directly, but the point of NSOperation is that it hides threading from you, leaving you to concentrate on the processing you need to do. Try firing NSOperation requests as and when required, see what the performance is like in your use-case; even if these operations get data in an async manner, it should provide you with a cleaner solution with good performance, plus future proof.
I've successfully used NSInvocationOperation to fire requests as often as required, and it sounds like the sort-of requirements and behaviour you're after. I would suggest more generally that you experiment with these in a test project; here you can test performance.
The following weblog's helped me start playing with NSOperation:
http://www.dribin.org/dave/blog/archives/2009/09/13/snowy_concurrent_operations/
http://www.cimgf.com/2008/02/16/cocoa-tutorial-nsoperation-and-nsoperationqueue/
As always, the Apple Threading Programming Guide is a key read, to figure out which way to go depending on needs.
This sounds like an ideal job for an NSOperationQueue. Have a read of the operation queue section of the concurrency guide.
Essentially, you create an NSOperation object for each map tile load and place them on a queue that only allows them to execute one at a time.
Put a run loop in your background compute thread. Then use an NSOperation queue to manage sending messages to it. The queue plus the run loop will serialize all the work requests for you.

NSTimer Lag - iPhone SDK

I made a game that uses many timers throughout the code. However the timer has to deal with many tasks in such a small amount of time which leads to the problem where there is lag in my game. For example, my timer runs at an interval of (0.05) and it needs to draw and update many of the images on the screen. Is there any way I can distribute the work flow so that the program runs much smoother?
Thanks
Kevin
I would use an NSThread instead of an NSTimer. I have had more success in this area using NSThread because it runs on an independant thread and is not fired off your main ui thread. In the loop for the thread sleep it for 1/20 (your 0.05) of a second. Because the thread is not running on the UI thread all of its tasks should not slow your UI down. However beacsue it is not running on the UI you will have to call performSelectorOnMainThread to get the UI to update from this background thread. I put a lock on my update method (a simple boolean) that says if the last ui update has not happened, just skip this one. then if im running out of processing time i just drop a frame or two here and there. I also do a lot of checking to see if anything has actually changed before i redraw.
Simple solution: Ditch NSTimer.
Move your redrawing code to a single method, then use CADisplayLink. The issue with using your NSTimer approach is that everything is being redrawn too fast or too slow for the screen. By using CADisplayLink, you can synchronize your redraw code to the screen refresh rate. All you need to do then is touch up your code so that it can deal with not being called at a specific time.
And yes, check to make sure you don't need to redraw as Aran Mulholland said above. Just make sure the checks don't take as long as a redraw.
And remember to optimize your code. A lot. Use ivars to access objects, but the whole property (self.myObject =) to set your objects.