0

I am trying to make this runloop run forever(if not forever atleast for a single day) I am using the following function for runloop [self.runLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:100000]];

The problem is that the app crashes after 3 hours.Can anybody help me?

zzzzz
  • 1,209
  • 2
  • 18
  • 45

2 Answers2

2

If you want it to run perpetually (and assuming you've attached timers and/or input sources), you can use

[self.runLoop run];

If you want to stay with your current construct, you can use:

[self.runLoop runUntilDate:[NSDate distantFuture]];

I've seen Apple use the following pattern (e.g. listing 3-14 of the Threading Programming Guide; I've also seen NSOperation objects use this construct when invoking an asynchronous operation that they want to wait to complete):

// Let the run loop process things.
do
{
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
                             beforeDate:[NSDate distantFuture]];
}
while ( ... ); // in here you have whatever condition you want to check for
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • why do i need a while block? I dont need to check any condition.I want the run loop to run forever.How do i add your infor to my line of code? – zzzzz Mar 21 '13 at 06:57
  • @iOsBoy If you want it to run forever, you can just invoke `run` or `runUntilDate:[NSDate distantFuture]` (assuming you've attached a timer and/or input source). The while loop and `runMode` construct is useful if you're going to terminate upon some condition. Obviously you could use `while(TRUE)`, but the other constructs probably make more sense at that point. – Rob Mar 21 '13 at 12:57
1

Try [NSDate distantFuture]. From the documentation: "An NSDate object representing a date in the distant future (in terms of centuries)."

fumoboy007
  • 5,345
  • 4
  • 32
  • 49