I am trying to connect to a port on a given IP Address. The one problem is that when a connection is established with a non existent IP Address the write command (which is as follows):
NSData * imageRequest = [@"640" dataUsingEncoding: NSUTF8StringEncoding];
int image = [self.outputImageStream write:[imageRequest bytes] maxLength:[imageRequest length]];
It takes over 75 seconds to respond. I try dealing with this by starting a timer with the following method call:
self.connectionTimeoutTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(respondToTimer:)userInfo:nil repeats:NO];
It is not called within the 1 second interval I define.
Is there any way to ensure that the timer fires within 1 second?
This might answer your question about NSTimer real-time behavior:
You don't specify how you are scheduling the NSTimer, but you might find some improvements in its performance if you schedule it for
NSRunLoopCommonModes
instead ofNSRunLoopDefaultModes
:If this does not improve things, then you should look for lower-level alternatives.
In particular, you might look into
CADisplayLink
:which can be used in a quite similar fashion as NSTimer.
The advantage of CADisplayLink is that it is linked to the refresh rate, so it should give pretty reliable behavior for 1/60 sec time interval.
Example of use of CADisplayLink:
This will call
respondTimer
each 60 refresh frames, i.e., once per second. Since you want your method to be called just one, you could use:and it will be possibly easier for you. (Of course you could replace the call to [self respondToTimer] with the full implementation for that method).