How can I consolidate deferred/delayed calls in Objective-C ?

Posted by thrusty on Stack Overflow See other posts from Stack Overflow or by thrusty
Published on 2010-06-03T17:22:34Z Indexed on 2010/06/03 17:24 UTC
Read the original article Hit count: 239

I'd like to ensure that certain maintenance tasks are executed "eventually". For example, after I detect that some resources might no longer be used in a cache, I might call:

[self performSelector:@selector(cleanupCache) withObject:nil afterDelay:0.5];

However, there might be numerous places where I detect this, and I don't want to be calling cleanupCache continuously. I'd like to consolidate multiple calls to cleanupCache so that we only periodically get ONE call to cleanupCache.

Here's what I've come up with do to this-- is this the best way?

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(cleanupCache) object:nil]; [self performSelector:@selector(cleanupCache) withObject:nil afterDelay:0.5];

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about iphone-sdk