0

I am scheduling a method to be called with an object in the near future and the object is just a random NSString that is gone as soon as I schedule the selector.

So I may say something like:

[self performSelector:@selector(runMethod:) withObject:@"randomString" afterDelay:1.0f];


If I need to cancel this BEFORE it fires documentation says to use:

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(runMethod:) object:***];
  • • *The only issue is I don't know what the "object" is, it was just a random string that doesn't exist anymore and has been released by ARC by now. How can I cancel any scheduled methods with a specific selector (in my case runMethod:) but without knowing the "object"?

Is there any way to get a list of all scheduled functions in the NSRunLoop and just iterate through them with a for loop looking for ones with specific selector names?

Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195

2 Answers2

0

If you need to cancel things you should organise a better way to schedule them such that you can actually check what it scheduled and the details associated with it. A potential solution would be a custom class with a set of parameters. Internally this class runs a timer which executes the action at the specified fire time. An array of instances of this class would be trivial to search and cancel arbitrary items from.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • I was hoping to avoid building an engine for it heh. Yeah I think this is definitely do-able, do you know if there is just any way to look through everything in the NSRunLoops though and pull them out of that based on the selector's name? – Albert Renshaw May 20 '16 at 23:01
  • I don't think so, but I can't say I've searched extensively – Wain May 21 '16 at 07:10
-1

If, as I understand, the randomString is useless for you, then pass nil to both performSelector... and cancelPreviousPerform...

Like this:

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(runMethod) object:nil];

[self performSelector:@selector(runMethod) withObject:nil afterDelay:1.0f];
Shebuka
  • 3,148
  • 1
  • 26
  • 43