forked from mikeash/MAZeroingWeakRef
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
MAWeakTimer additions.
Allows the creation and scheduling of timers that do not retain their targets and invalidate themselves automatically when the target is deallocated.
- Loading branch information
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// NSTimer+MAWeakTimer.h | ||
// ZeroingWeakRef | ||
// | ||
// Created by Remy Demarest on 27/10/2011. | ||
// Copyright (c) 2011 NuLayer Inc. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface NSTimer (MAWeakTimer) | ||
+ (NSTimer *)scheduledWeakTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats; | ||
+ (NSTimer *)weakTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats; | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// NSTimer+MAWeakTimer.m | ||
// ZeroingWeakRef | ||
// | ||
// Created by Remy Demarest on 27/10/2011. | ||
// Copyright (c) 2011 NuLayer Inc. All rights reserved. | ||
// | ||
|
||
#import "NSTimer+MAWeakTimer.h" | ||
#import "MAZeroingWeakRef.h" | ||
#import "MAZeroingWeakProxy.h" | ||
|
||
@implementation NSTimer (MAWeakTimer) | ||
|
||
+ (NSTimer *)scheduledWeakTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats; | ||
{ | ||
MAZeroingWeakProxy *proxy = [MAZeroingWeakProxy proxyWithTarget:target]; | ||
|
||
NSTimer *timer = [self scheduledTimerWithTimeInterval:seconds target:proxy selector:aSelector userInfo:userInfo repeats:repeats]; | ||
|
||
MAWeakDeclare(timer); | ||
|
||
[proxy setCleanupBlock: | ||
^(id target) { | ||
MAWeakImportReturn(timer); | ||
|
||
[timer invalidate]; | ||
}]; | ||
|
||
return timer; | ||
} | ||
|
||
+ (NSTimer *)weakTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats; | ||
{ | ||
MAZeroingWeakProxy *proxy = [MAZeroingWeakProxy proxyWithTarget:target]; | ||
|
||
NSTimer *timer = [self timerWithTimeInterval:seconds target:proxy selector:aSelector userInfo:userInfo repeats:repeats]; | ||
|
||
MAWeakDeclare(timer); | ||
|
||
[proxy setCleanupBlock: | ||
^(id target) { | ||
MAWeakImportReturn(timer); | ||
|
||
[timer invalidate]; | ||
}]; | ||
|
||
return timer; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters