performSelector:withObject:afterDelay: not queuing selector

I've setup a block to run on a different queue and call another method after a delay:

piemanQ = dispatch_queue_create(PIEMAN_QUEUE_NAME, NULL);
dispatch_async(piemanQ, ^{
    [self performSelector:@selector(sendReadyToPieman) withObject:nil afterDelay:1.0];
});

I expect that a second later the @selector(sendReadyToPieman) fires, however nothing happens. I've read through the doco on the performSelector:withObject:afterDelay: and it talks about the method being added via a timer on the current queue. I've checked the current queues run loop mode, but it returns nil.

I'm sure I've done this sort of code before, but I've tried this in two different places and in both cases it has not run. But if I replace it with a dispatch_after(...) everything works.

Can anyone shed some light?

Answers


You should put the operation on an NSOperationQueue instead:

NSOperationQueue *piemanQ = [[NSOperationQueue alloc] init];
piemanQ.name = @"some name";
[piemanQ addOperationWithBlock:^{
    [self performSelector:@selector(sendReadyToPieman) withObject:nil afterDelay:1.0];
}];

This is automatically asynchronous. It is better to use an Objective-C solution over a C solution to a problem.


My guess is: dispatch_async does what it says, it makes the following stuff run async. You queue your selector, then the block is finished and the whole async thing goes away. Including the queued selector, of course.

For the performSelector to do anything, the thread needs to be alive, and it needs to execute the runloop.


Need Your Help

how to capture screenshot on Motorola Xoom (android)?

android xoom

I have a Motorola Xoom tablet of Android and I am facing the problem to take the screenshots of my application for demo purposes but haven't succeeded yet... can anyone guide me? Is there any short...

objective c iphone app not working as planned (table-view app)

iphone objective-c uitableview

i've inserted a view before an established table view if that makes sense. ie. i had an app with a first and second view, then i put another view before the first view, now the content doesnt show ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.