Is there a setTimeout (like) in objective-c - objective-c

Is there a similar setTimeout function in objective-c like in javascript? I dont want a sleep function.
I want to fire a "message" after a period of time gone by but no locking up the device.

Try NSTimer.

As Calvin points you can use NSTimer class.
As an alternative you can use performSelector:withObject:afterDelay: method to schedule method call after specific delay (note that it is not guaranteed that delay will be exactly you specified - it just will be not less than that value)

See NSObject's performSelector:withObject:afterDelay:. You can also use NSTimer — it's particularly useful for mimicking setInterval(). And to execute arbitrary code instead of just sending a message, see Grand Central Dispatch's dispatch_after.

Related

sequential methods execution obj-c

Very beginner obj-c question.
My task is to do simple sequence when view loads:
pause 2 secons
method1 execution
pause 2 seconds
method2 execution
pause 2 seconds
method3 execution
I write this code for iOS5+ so blocks or other features can be used. I tried
[NSThread sleepForTimeInterval:x];
to make the pauses between methods executions, but three pauses added to one and I have one big pause, so it's wrong piece.
How to do this in right way?
Sleeping an NSThread is never the right way to handle timed method execution. You want NSTimer, which you can set to either fire the same method after two seconds (then have that method determine which selector to perform next), or have three timers set to fire two seconds after each other, each calling a different method.
You could use performSelector:withObject:afterDelay:, or as CodaFi said, an NSTimer. Either method allows to to pass an object to the method, and that object could be an array or dictionary, if you need to pass multiple arguments.
I'd chain the methods:
call 1. method and make it call 2.
method should call 3.
should proceed to 4.
...

Is there a simple way (in Cocoa/iOS) to queue a method call to run once in the next run loop?

UIView has a setNeedsDisplay method that one can call several times within the same event loop, safe in the knowledge that the redrawing work will happen soon, and only the once.
Is there a generic mechanism for this sort of behaviour Cocoa? A way of of saying, "Queue a selector as many times as you like, when it's time, the selector will run once & clear the queue."
I know I could do this with some kind of state tracking in my target, or with an NSOperationQueue. I'm just wondering if there's a lightweight approach I've missed.
(Of course, the answer may be, "No".)
setNeedsDisplay is not a good example of what you're describing, since it actually does run every time you call it. It just sets a flag. But the question is good.
One solution is to use NSNotificationQueue with NSNotificationCoalescingOnName.
Another solution is to build a trampoline to do the coalescing yourself. I don't have a really good blog reference for trampolines, but here's an example of one (LSTrampoline). It's not that hard to build this if you want to coalesce the messages over a period of time. I once built a trampoline with a forwardInvocation: similar to this:
- (void)forwardInvocation:(NSInvocation *)invocation {
[invocation setTarget:self.target];
[invocation retainArguments];
[self.timer invalidate];
self.timer = [NSTimer scheduledTimerWithTimeInterval:self.timeout invocation:invocation repeats:NO];
}
This actually coalesces all messages to the object over the time period (not just matching messages). That's all I needed for the particular problem. But you could expand on this to keep track of which selectors are being coalesced, and check your invocations to see if they match "sufficiently."
To get this to run on the next event loop, just set timeout to 0.
I keep meaning to blog about trampolines. Required shilling: My upcoming book covers trampolines in Chapter 4 and Chapter 20.
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:#selector(doTheThing:)
object:someObject];
[self performSelector:#selector(doTheThing:)
withObject:someObject
afterDelay:0];
This is not exactly how UIView is doing it because setNeedsDisplay simply sets a flag and the internal UIView mechanism makes sure to call drawRect: after setting up the drawing environment, but this is a generic way and doesn't require any special state tracking in your class.

How to wait for an event or timeout

I'm trying a simple thread program.
I need to wait for a event and time delay. This time delay is varying. How to do this?
Can anyone explain with a sample program? I know how to spawn a thread with NSThreads as well as through NSInvocationOperation.
If you need to wait for a timeout consider using NSTimer. You can use setFireDate method to modify time delay.
For asynchronous events there's a lot of way to solve the problem, according to what you need in your program: first of all you may simply setting a delegate that receive a message when something happen in your class. If you need something more complex (in order to avoid race conditions or deadlocks) you may consider use NSNotificationCenter or NSConnection.

order of execution in run loop

I am using [NSTimer scheduledTimerWithInterval:selector:#selector(loop):repeats:YES] to create a neverending loop with -loop() being called every interval. My viewcontroller also has a -touchesBegan to process UITouch's.
Suppose the screen is touched and a UIEvent is fired, does the code in -touchesBegan execute first in the run loop or the code in the -loop called by NSTimer execute first?
It's explained in this Apple document.
However, it's not that useful. NSTimer is not a real-time mechanism, and you can't predict exactly when it fires. It will also depend how often iOS registers touch events, and how often you configure the timer to fire. It would be a very, very, very bad idea to write a code which depends on these subtleties and features buried deep in the documentation I just cited.
Write code which doesn't depend on what you just asked.

Objective-C class time delay?

I was wondering if anyone knows of a class in objective-C that stops/delays the application at a specified amount of time during runtime? I've already looked at the Time Manager and NSTimer and don't see how I could possibly put a delay in there. I've never used these two classes before though, so maybe someone else has and could give me a tip? Thanks!
It really depends what you mean by "stops/delays the app." If you have a single threaded app you can just use anyone of the various sleep calls (usleep, sleep, nanosleep, etc). That is probably not what you want though, because it will just result in a hung UI for the user, and won't stop any background threads.
To actually give a decent answer it is probably necessary to know what you are actually trying to do. Are you waiting for something, polling a resource, etc?
#include <unistd.h>
sleep(3); // or usleep(3000000);
I think you're looking for [NSThread sleepForTimeInterval:] That should do the trick. You should also consider using a timer to invoke another function after a delay, because that solution is non-blocking. The NSThread approach will totally lock your application for the time interval if you call it from the main thread.