A proxy object for NSTimer that avoids retain cycles
Retain cycles are annoying to deal with. As explained here.
The problem lies in the fact that if MyViewController becomes a NSTimer's target, it is retained by the NSTimer. And so if MyViewController also happens to own the NSTimer, a retain cycle is created.
To fix this problem, we use a proxy object MSTimer which takes the burden off of MyViewController. So when you dismiss MyViewController, its dealloc gets called as expected.
This is accomplished by using message forwarding. Meaning our MSTimer will forward any messages/methods to the underlying NSTimer.
Instantiate using class method, as you would a regular NSTimer. NOTE: this adds the timer to the runloop with specified mode right away.
To create one:
MSTimer *timer = [MSTimer startTimerWithTimeInterval:1 target:self selector:@selector(doSomething) userInfo:nil repeats:YES runLoop:[NSRunLoop currentRunLoop] mode:NSDefaultRunLoopMode];
Treat the timer object as if it was a NSTimer:
[timer isValid];
[timer setFireDate:[NSDate date]];
...
To clean up (important!!):
[timer invalidate];
[timer release];
MSTimer is available under the MIT license. See the LICENSE file for more info.