Question

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?

Était-ce utile?

La solution

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.

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top