views:

77

answers:

1

I am using the following code to perform a selector after a delay with multiple passed parameters:

http://nifty-box.com/blog/2006/12/nsinvocation-cleans-code.html

It works very well, but I need to extend this to support the equivalent of:

[NSObject cancelPreviousPerformRequestsWithTarget:self]

(target in this case would not be self, but would be an instance of _UFLatePerformer I believe)

Is there any way to do this, so my view's deallocation can kill all remaining delayed performance requests?

+2  A: 

On the mac you would be able to use objc_setAssociatedObject, but that does not exist on the iPhone.

So basically you need some kind of manager singleton that holds onto these references for you that you pass in an NSObject and it does what you want.

When I was doing something similar I wrote a singleton class that took in requests to perform an action, and started a timer that fired when the action was to be done - at that time my manager class decided if the request in question was still valid, and if so fired the action. I could thus effectively cancel a timer, and furthermore could tell the manager to reset the timer which didn't really, it just made a new timer but when the old timer fired the action it was requested to perform was invalid.

So basically the timer was calling the singleton back with just a request ID, which led to a dictionary entry containing details of the call.

Kendall Helmstetter Gelner
That was the only solution I could think of as well, but I was really trying to avoid creating a singleton and managing lists of selectors. Once I decided that I *was* going to have to go to the trouble, I just got rid of my over-riding NSObject, and stuck all of my variables into a single NSDictionary. That way, I was able to just use the standard performSelector:withObject:afterDelay -- which meant that cancelPreviousPerformRequestsWithTarget:self would work as expected.
Jeffrey Berthiaume