Skip to content

bogardon/MSTimer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MSTimer

A proxy object for NSTimer that avoids retain cycles

Why

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.

How To Use

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];

License

MSTimer is available under the MIT license. See the LICENSE file for more info.

About

A proxy object for NSTimer that avoids retain cycles

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published