0

I'm trying to figure out how NSRunLoop works. So there are a few delayed tasks and I want to perform them in a few seconds using NSRunLoop. And I want to create NSRunLoop manually. How am I supposed to do this?

NSRunLoop *loop = [NSRunLoop currentRunLoop];

//create delayed tasks
[object performSelector:NSSelectorFromString(@"firstMethod") withObject:firstArgument afterDelay:5.0];
[object performSelector:NSSelectorFromString(@"secondMethod") withObject:secondArgument afterDelay:3.0];

//and here I must run a loop 
while(flag&&[loop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);

And also I have somehow to stop this loop. As you see I'm totally confused and lots of Apple's documentation and topics here didn't help me.

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
Tracy Jr.
  • 27
  • 4
  • Is this a thread that you created yourself? Why are you trying to spin the run loop manually? What's wrong with the code you've posted? – jscs Jul 29 '15 at 21:40
  • @JoshCaswell, this is a main thread. And I've read that it's not necesary to create main thread's runloop, but as I said I want to figure out how it works. As for code. It gets to infinite loop in `while` and nothing then happens. – Tracy Jr. Jul 29 '15 at 21:52
  • The main run loop is created and spun automatically. There's sometimes reason to spin it yourself, but this doesn't seem to be it. Your `performSelector:...` calls will take effect without you touching the run loop. – jscs Jul 29 '15 at 21:53
  • @JoshCaswell Okay, but the program's executed faster than delayed task is supposed to. So how to make it wait for `performSelector`? – Tracy Jr. Jul 29 '15 at 22:02
  • `firstMethod` and `secondMethod` are called too soon? – jscs Jul 29 '15 at 22:09
  • @JoshCaswell, they are not called at all – Tracy Jr. Jul 29 '15 at 22:09
  • Thanks for your help. Hope to solve the problem – Tracy Jr. Jul 29 '15 at 22:21

1 Answers1

0

To the Q and the comments:

Of course, they are never performed. -performSelector:… is attached to the thread's current run loop. This run loop is never reached, because your program get stuck in your private run loop.

Moreover, it is simply not possible, to attach a run loop to a thread:

Your application cannot either create or explicitly manage NSRunLoop objects. Each NSThread object, including the application’s main thread, has an NSRunLoop object automatically created for it as needed. If you need to access the current thread’s run loop, you do so with the class method currentRunLoop.

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSRunLoop_Class/index.html

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
  • There's no "private" run loop. The loop that's being spun _is_ the thread's run loop, and it's being run in default mode, which is the mode that `performSelector:...` will execute its argument method in. – jscs Aug 05 '15 at 20:16
  • @JoshCaswell Wow, I really read `+alloc-init`, may because of the text above. My fault. I will delete my answer. – Amin Negm-Awad Aug 06 '15 at 06:08