Skip to content

Commit

Permalink
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
PsychoH13 committed Oct 27, 2011
1 parent 521cf98 commit 3ec5a05
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Source/NSTimer+MAWeakTimer.h
@@ -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
51 changes: 51 additions & 0 deletions Source/NSTimer+MAWeakTimer.m
@@ -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
6 changes: 6 additions & 0 deletions ZeroingWeakRef.xcodeproj/project.pbxproj
Expand Up @@ -14,6 +14,7 @@
C2D7541011E2588600816068 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = C2D7540F11E2588600816068 /* main.m */; };
C2DF1F2611ED2C1300EFC8AE /* MAWeakArray.m in Sources */ = {isa = PBXBuildFile; fileRef = C2DF1F2511ED2C1300EFC8AE /* MAWeakArray.m */; };
C2DF1F2B11ED2C4700EFC8AE /* MAWeakDictionary.m in Sources */ = {isa = PBXBuildFile; fileRef = C2DF1F2A11ED2C4700EFC8AE /* MAWeakDictionary.m */; };
C65F68161459990300D3A417 /* NSTimer+MAWeakTimer.m in Sources */ = {isa = PBXBuildFile; fileRef = C65F68151459990300D3A417 /* NSTimer+MAWeakTimer.m */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -43,6 +44,8 @@
C2DF1F2511ED2C1300EFC8AE /* MAWeakArray.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MAWeakArray.m; sourceTree = "<group>"; };
C2DF1F2911ED2C4700EFC8AE /* MAWeakDictionary.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MAWeakDictionary.h; sourceTree = "<group>"; };
C2DF1F2A11ED2C4700EFC8AE /* MAWeakDictionary.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MAWeakDictionary.m; sourceTree = "<group>"; };
C65F68141459990300D3A417 /* NSTimer+MAWeakTimer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSTimer+MAWeakTimer.h"; sourceTree = "<group>"; };
C65F68151459990300D3A417 /* NSTimer+MAWeakTimer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSTimer+MAWeakTimer.m"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -81,6 +84,8 @@
C2DF1F2511ED2C1300EFC8AE /* MAWeakArray.m */,
C2DF1F2911ED2C4700EFC8AE /* MAWeakDictionary.h */,
C2DF1F2A11ED2C4700EFC8AE /* MAWeakDictionary.m */,
C65F68141459990300D3A417 /* NSTimer+MAWeakTimer.h */,
C65F68151459990300D3A417 /* NSTimer+MAWeakTimer.m */,
);
path = Source;
sourceTree = "<group>";
Expand Down Expand Up @@ -165,6 +170,7 @@
C2DF1F2611ED2C1300EFC8AE /* MAWeakArray.m in Sources */,
C2DF1F2B11ED2C4700EFC8AE /* MAWeakDictionary.m in Sources */,
C26F610511F207270080EC96 /* MAZeroingWeakProxy.m in Sources */,
C65F68161459990300D3A417 /* NSTimer+MAWeakTimer.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit 3ec5a05

Please sign in to comment.