From 6785e2c0a612ca02c3ff687b67c0ffec84b22014 Mon Sep 17 00:00:00 2001 From: _Peng Wang Date: Wed, 14 Dec 2016 16:23:48 +0800 Subject: [PATCH 1/2] solve nastier retain cycle --- MZTimerLabel/MZTimerLabel.m | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/MZTimerLabel/MZTimerLabel.m b/MZTimerLabel/MZTimerLabel.m index ee33133..4b635bf 100644 --- a/MZTimerLabel/MZTimerLabel.m +++ b/MZTimerLabel/MZTimerLabel.m @@ -243,10 +243,28 @@ -(void)start{ _timer = nil; } + __weak typeof(self)weakself = self; + if ([self.timeFormat rangeOfString:@"SS"].location != NSNotFound) { - _timer = [NSTimer scheduledTimerWithTimeInterval:kDefaultFireIntervalHighUse target:self selector:@selector(updateLabel) userInfo:nil repeats:YES]; + + _timer = [NSTimer scheduledTimerWithTimeInterval:kDefaultFireIntervalHighUse + repeats:YES + block:^(NSTimer * _Nonnull timer) { + __strong typeof(weakself)strongself = weakself; + if (strongself) { + [strongself updateLabel]; + } + }]; }else{ - _timer = [NSTimer scheduledTimerWithTimeInterval:kDefaultFireIntervalNormal target:self selector:@selector(updateLabel) userInfo:nil repeats:YES]; + + _timer = [NSTimer scheduledTimerWithTimeInterval:kDefaultFireIntervalNormal + repeats:YES + block:^(NSTimer * _Nonnull timer) { + __strong typeof(weakself)strongself = weakself; + if (strongself) { + [strongself updateLabel]; + } + }]; } [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes]; From b866e99468ea0f930d1f12c1f477a2deea3802e7 Mon Sep 17 00:00:00 2001 From: _Peng Wang Date: Wed, 14 Dec 2016 16:50:34 +0800 Subject: [PATCH 2/2] nstimer solve retain cycle MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit below iOS 10.0 achive it bymyself ,or use system api --- MZTimerLabel.xcodeproj/project.pbxproj | 8 ++++++ MZTimerLabel/MZTimerLabel.m | 38 ++++++++++++++++---------- MZTimerLabel/NSTimer+Block.h | 23 ++++++++++++++++ MZTimerLabel/NSTimer+Block.m | 30 ++++++++++++++++++++ 4 files changed, 85 insertions(+), 14 deletions(-) create mode 100644 MZTimerLabel/NSTimer+Block.h create mode 100644 MZTimerLabel/NSTimer+Block.m diff --git a/MZTimerLabel.xcodeproj/project.pbxproj b/MZTimerLabel.xcodeproj/project.pbxproj index c269ade..7d8c54e 100644 --- a/MZTimerLabel.xcodeproj/project.pbxproj +++ b/MZTimerLabel.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 279565FD1E013A1700ABA8B6 /* NSTimer+Block.h in Headers */ = {isa = PBXBuildFile; fileRef = 279565FB1E013A1700ABA8B6 /* NSTimer+Block.h */; }; + 279565FE1E013A1700ABA8B6 /* NSTimer+Block.m in Sources */ = {isa = PBXBuildFile; fileRef = 279565FC1E013A1700ABA8B6 /* NSTimer+Block.m */; }; 40A965371AD4CDB700B2D499 /* MZTimerLabel.h in Headers */ = {isa = PBXBuildFile; fileRef = 40A965111AD4CAF500B2D499 /* MZTimerLabel.h */; settings = {ATTRIBUTES = (Public, ); }; }; 40A965381AD4CDBD00B2D499 /* MZTimerLabel.m in Sources */ = {isa = PBXBuildFile; fileRef = 40A965121AD4CAF500B2D499 /* MZTimerLabel.m */; }; /* End PBXBuildFile section */ @@ -22,6 +24,8 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 279565FB1E013A1700ABA8B6 /* NSTimer+Block.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSTimer+Block.h"; sourceTree = ""; }; + 279565FC1E013A1700ABA8B6 /* NSTimer+Block.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSTimer+Block.m"; sourceTree = ""; }; 40A965111AD4CAF500B2D499 /* MZTimerLabel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MZTimerLabel.h; sourceTree = ""; }; 40A965121AD4CAF500B2D499 /* MZTimerLabel.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MZTimerLabel.m; sourceTree = ""; }; 40A9651A1AD4CB5F00B2D499 /* MZTimerLabel.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = MZTimerLabel.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -67,6 +71,8 @@ 40A9651C1AD4CB5F00B2D499 /* MZTimerLabel */ = { isa = PBXGroup; children = ( + 279565FB1E013A1700ABA8B6 /* NSTimer+Block.h */, + 279565FC1E013A1700ABA8B6 /* NSTimer+Block.m */, 40A965111AD4CAF500B2D499 /* MZTimerLabel.h */, 40A965121AD4CAF500B2D499 /* MZTimerLabel.m */, 40A9651D1AD4CB5F00B2D499 /* Supporting Files */, @@ -90,6 +96,7 @@ buildActionMask = 2147483647; files = ( 40A965371AD4CDB700B2D499 /* MZTimerLabel.h in Headers */, + 279565FD1E013A1700ABA8B6 /* NSTimer+Block.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -188,6 +195,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 279565FE1E013A1700ABA8B6 /* NSTimer+Block.m in Sources */, 40A965381AD4CDBD00B2D499 /* MZTimerLabel.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/MZTimerLabel/MZTimerLabel.m b/MZTimerLabel/MZTimerLabel.m index 4b635bf..63768bc 100644 --- a/MZTimerLabel/MZTimerLabel.m +++ b/MZTimerLabel/MZTimerLabel.m @@ -27,7 +27,7 @@ // THE SOFTWARE. #import "MZTimerLabel.h" - +#import "NSTimer+Block.h" #define kDefaultTimeFormat @"HH:mm:ss" #define kHourFormatReplace @"!!!*" @@ -243,21 +243,21 @@ -(void)start{ _timer = nil; } - __weak typeof(self)weakself = self; + CGFloat precision; - if ([self.timeFormat rangeOfString:@"SS"].location != NSNotFound) { - - _timer = [NSTimer scheduledTimerWithTimeInterval:kDefaultFireIntervalHighUse - repeats:YES - block:^(NSTimer * _Nonnull timer) { - __strong typeof(weakself)strongself = weakself; - if (strongself) { - [strongself updateLabel]; - } - }]; + if ([self.timeFormat rangeOfString:@"SS"].location != NSNotFound){ + + precision = kDefaultFireIntervalHighUse; }else{ - - _timer = [NSTimer scheduledTimerWithTimeInterval:kDefaultFireIntervalNormal + + precision = kDefaultFireIntervalNormal; + } + + __weak typeof(self)weakself = self; + + if ([NSTimer respondsToSelector:@selector(scheduledTimerWithTimeInterval:repeats:block:)]) { + + _timer = [NSTimer scheduledTimerWithTimeInterval:precision repeats:YES block:^(NSTimer * _Nonnull timer) { __strong typeof(weakself)strongself = weakself; @@ -265,6 +265,16 @@ -(void)start{ [strongself updateLabel]; } }]; + }else{ + + _timer = [NSTimer scheduledTimerWithTimeInterval:precision + block:^{ + __strong typeof(weakself)strongself = weakself; + if (strongself) { + [strongself updateLabel]; + } + } + repeats:YES]; } [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes]; diff --git a/MZTimerLabel/NSTimer+Block.h b/MZTimerLabel/NSTimer+Block.h new file mode 100644 index 0000000..541c139 --- /dev/null +++ b/MZTimerLabel/NSTimer+Block.h @@ -0,0 +1,23 @@ +// +// NSTimer+Block.h +// SuperCalculator +// +// Created by Peng Wang on 2016/12/14. +// Copyright © 2016年 youdao. All rights reserved. +// + +#import + + +/** + NSTimer (Block) is used to solving retain cycle below ios10.0 + + if is ios10.0 ,we can use method: + + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0)); + + */ +@interface NSTimer (Block) + ++ (NSTimer*)scheduledTimerWithTimeInterval:(NSTimeInterval)interval block:(void(^)())block repeats:(BOOL)repeats; + +@end diff --git a/MZTimerLabel/NSTimer+Block.m b/MZTimerLabel/NSTimer+Block.m new file mode 100644 index 0000000..45ce4fe --- /dev/null +++ b/MZTimerLabel/NSTimer+Block.m @@ -0,0 +1,30 @@ +// +// NSTimer+Block.m +// SuperCalculator +// +// Created by Peng Wang on 2016/12/14. +// Copyright © 2016年 youdao. All rights reserved. +// + +#import "NSTimer+Block.h" + +@implementation NSTimer (Block) + ++ (NSTimer*)scheduledTimerWithTimeInterval:(NSTimeInterval)interval block:(void(^)())block repeats:(BOOL)repeats{ + + return [self scheduledTimerWithTimeInterval:interval + target:self + selector:@selector(blockInvoke:) + userInfo:[block copy] + repeats:repeats]; +} + ++ (void)blockInvoke:(NSTimer*)timer { + + void (^block)() = timer.userInfo; + if (block) { + block(); + } +} + +@end