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
Handling Push Notifications When App Is Terminated
Swift Version of Componentsseparatedbystring
Restricting App Installations from Appstore Only to Users with iPhone 5/5S/5C
Changing Placeholder Text Color with Swift
How to Prevent the iPhone Screen from Dimming or Turning Off While My Application Is Running
Uicollectionview Decoration View
Distancefromlocation - Calculate Distance Between Two Points
Hiding the Tabbar and Removing the Space
Custom Font Not Working Programmatically in Swift
Objective-C Get a Class Property from String
Use a Generic Class as a Custom View in Interface Builder
-Canopenurl: Failed for Url: "Fbauth2:/" (Osstatus Error -10814.)"
Getting Time Elapsed in Objective-C
How to Disable/Enable the Sleep Mode Programmatically in iOS
Ios4: How to Use Video File as an Opengl Texture