Question

I have a modal view controller that appears, checks a service on the Internet and then dismisses itself when done. The nib contains an activity indicator and a label to inform the user what is going on.

When the update is complete, the label changes to "Update Complete" and then dismisses the view controller. However, I want it to delay the dismiss for a couple of seconds to give the user a chance to see the text before it disappears. So I've done this:

#pragma mark - AssetLoaderServiceDelegate

- (void)assetLoaderServiceDidFinishLoading:(AssetLoaderService *)service
{
    [self.spinner stopAnimating];
    self.infoLabel.text = @"Update complete";
    [self performSelector:@selector(dismissUpdater) withObject:nil afterDelay:2.0];
}

- (void)dismissUpdater
{
    [self dismissModalViewControllerAnimated:YES];
}

But for some reason, the selector is never being called. I've tried running it in mode NSRunLoopCommonModes too, but that doesn't work either.

I must be doing something wrong, but I can't see what...

EDIT: The delegate callback is actually happening within an NSOperationQueue, which might mean it's not on the same thread when it sends the message back to the view controller? So I tried

[self performSelector:@selector(downloadQueueComplete) withObject:nil afterDelay:0.0 inModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];

followed by

- (void)downloadQueueComplete
{
    [delegate assetLoaderServiceDidFinishLoading:self];
}

But the performSelector doesn't seem to be working here either.

Was it helpful?

Solution

Following up your suggestion about the thread issue, would you try with:

[self performSelectorOnMainThread:@selector(downloadQueueComplete) withObject:nil waitUntilDone:YES]];

OTHER TIPS

Sorted! In the AssetLoaderService, I had to perform selector on main thread:

 [self performSelectorOnMainThread:@selector(downloadQueueComplete) withObject:nil waitUntilDone:YES];

After that, all the following calls were on the correct thread. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top