Swift Performselector:Withobject:Afterdelay: Is Unavailable

IOS - performSelector:withObject:afterDelay: NOT WORKING

When I had this kind of thing happen, I was calling performSelector from a GCD dispatch. So it was setting the timer in the GCD worker thread which went away before the timer fired. When GCD removed the worker thread, the timer was lost, so the selector was never called.

performSelector:withObject:afterDelay: is not passing an argument as expected

perform doesn't work with parameters of primitive types like Int.

A better solution is to use DispatchQueue asyncAfter. This allows you to call the method normally.

func runOnRenderAnimations() -> Void {
DispatchQueue.main.asyncAfter(deadline: now() + 0.5) {
self.animateIntroViews(textPosition: 0)
}
DispatchQueue.main.asyncAfter(deadline: now() + 2.5) {
self.animateIntroViews(textPosition: 1)
}
}

why is performSelector not present in swift

Use

NSTimer.scheduledTimerWithTimeInterval(delay, target: self, selector: "onFlip", userInfo: nil, repeats: false)

instead. No idea why the other one is not available.

performSelector:withObject:afterDelay: not making call

Try using:

-(void) select {   
[self changeButtonNames];
[self drawMap];
[self performSelectorOnMainThread:@selector(showActionSheet) withObject:nil waitUntilDone:YES];
}

-performSelector:withObject:afterDelay: schedules a timer on the same thread to call the selector after the passed delay.

Maybe this will work for you:

-(void) select {   
[self changeButtonNames];
[self drawMap];
[self performSelectorOnMainThread:@selector(someA) withObject:nil waitUntilDone:YES];
}

-(void)someA {
[self performSelector:@selector(showActionSheet) withObject:nil afterDelay:2];
}

performSelector:withObject:afterDelay: not working from scrollViewDidZoom

I think that timer events are ignored during tracking (when a finger is down in order to scroll or zoom). You might have to perform the selector in a different mode (see [NSObject performSelector:withObject:afterDelay:inModes:]). Specifically, try using @[NSRunLoopCommonModes] for the mode.

performSelector:withObject:afterDelay: not working on modal view controller

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

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

performSelector:withObject:afterDelay from class method not working

Do you have a run loop running on the thread from which you invoke the perform:afterDelay:? If not, it won't run.

PerformSelector not working

The context where performSelector:withObject:afterDelay: is getting invoked is the culprit. Here's what's going on.

Some members of the performSelector... family, like this one, don't perform the selector right away; they queue up an invocation on the current run loop, so that it happens after your fn returns, the next go-round of the run loop. According to apple: "Specifying a delay of 0 does not necessarily cause the selector to be performed immediately. The selector is still queued on the thread’s run loop and performed as soon as possible."

Normally this is fine and expected. But your code is calling it on a thread that you started manually... and such threads don't keep their run loop going repeatedly the way the main thread does. They invoke the selector specified at creation once, and exit. So: your code queues up an invocation of your callback selector, but then the thread exits; and its run loop is thrown away without ever running... so your queued invocation never happens.

What you probably need is performSelectorOnMainThread:withObject:waitUntilDone:, since you may want the callback to happen on the thread that invoked the MyMethod method in the first place, which is presumably the main thread.

More generally, threading is very tricky stuff. I highly recommend checking out NSOperationQueue, NSBlockOperation, and related techniques - they can remove a great deal of the pain.



Related Topics



Leave a reply



Submit