美文网首页
OC 定时器的内存分析

OC 定时器的内存分析

作者: d5cbd4f07363 | 来源:发表于2017-07-07 23:02 被阅读35次

定时器用到项目上会有互相引用问题:直接上代码

#import@interface Person : NSObject

-(void)startTimer;

-(void)endTimer;

@end

#import "Person.h"

#import "NSTimer+HYBExtension.h"

@interface Person()

@property(nonatomic,retain)NSTimer *timer;

@end

@implementation Person

-(instancetype)init

{

self =[super init];

if (self) {

}

return self;

}

-(void)startTimer

{

__block Person * selfWeak = self;

//在这里先转成弱引用,防止定时器对person的强引用

NSTimer * timerC = [NSTimer scheduledTimerWithTimeInterval:2.0f repeats:YES callback:^{

// 换成强引用是为了调用方法时在并发时会使self变为nil

Person * StrongSelf = selfWeak;

[StrongSelf sayhello];

}];

self.timer = timerC;

//NSLog(@"ZKZTimer的引用计数:%ld",[_timer retainCount]);

}

-(void)saystr:(NSString *)ss

{

}

-(void)sayhello

{

NSLog(@"block");

//NSLog(@"---------------------------");

}

-(void)endTimer

{

[self.timer invalid];

}

-(void)dealloc

{

//    NSLog(@"person dealloc");

//    NSLog(@"%ld",[_Timer retainCount]);

//    [_Timer removeTimer];

NSLog(@"%ld",[_timer retainCount]);

if (_timer) {

[_timer release];

_timer = nil;

}

[super dealloc];

}

创建定时器分类(分类是在网上下载下来的)

@interface NSTimer (HYBExtension)

/**

*  无参数无返回值Block

*/

typedef void (^HYBVoidBlock)(void);

/**

*  创建Timer---Block版本

*

*  @param interval 每隔interval秒就回调一次callback

*  @param repeats  是否重复

*  @param callback 回调block

*

*  @return NSTimer对象

*/

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval

repeats:(BOOL)repeats

callback:(HYBVoidBlock)callback;

/**

*  创建Timer---Block版本

*

*  @param interval 每隔interval秒就回调一次callback

*  @param count  回调多少次后自动暂停,如果count <= 0,则表示无限次,否则表示具体的次数

*  @param callback 回调block

*

*  @return NSTimer对象

*/

//+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval

//                                      count:(NSInteger)count

//                                  callback:(HYBVoidBlock)callback;

/**

*  开始启动定时器

*/

- (void)fireTimer;

/**

*  Make it invalid if currently it is valid.

*/

- (void)invalid;

//

//  NSTimer+HYBExtension.m

//  HYBTimerExtension

//

//  Created by huangyibiao on 15/4/16.

//  Copyright (c) 2015年 huangyibiao. All rights reserved.

//

#import "NSTimer+HYBExtension.h"

@implementation NSTimer (HYBExtension)

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval

repeats:(BOOL)repeats

callback:(HYBVoidBlock)callback {

return [NSTimer scheduledTimerWithTimeInterval:interval

target:self

selector:@selector(onTimerUpdateBlock:)

userInfo:[callback copy]

repeats:repeats];

}

//+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval

//                                      count:(NSInteger)count

//                                  callback:(HYBVoidBlock)callback {

//  if (count <= 0) {

//    return [self scheduledTimerWithTimeInterval:interval repeats:YES callback:callback];

//  }

//

//  NSDictionary *userInfo = @{@"callback"    : [callback copy],

//                            @"count"        : @(count)};

//  return [NSTimer scheduledTimerWithTimeInterval:interval

//                                          target:self

//                                        selector:@selector(onTimerUpdateCountBlock:)

//                                        userInfo:userInfo

//                                        repeats:YES];

//}

+ (void)onTimerUpdateBlock:(NSTimer *)timer {

HYBVoidBlock block = timer.userInfo;

NSLog(@"分类引用计数:%ld",[timer retainCount]);

if (block) {

block();

}

}

+ (void)onTimerUpdateCountBlock:(NSTimer *)timer {

static NSUInteger currentCount = 0;

NSDictionary *userInfo = timer.userInfo;

HYBVoidBlock callback = userInfo[@"callback"];

NSNumber *count = userInfo[@"count"];

if (currentCount < count.integerValue) {

currentCount++;

if (callback) {

callback();

}

} else {

currentCount = 0;

[timer unfireTimer];

}

}

- (void)fireTimer {

[self setFireDate:[NSDate distantPast]];

}

- (void)unfireTimer {

[self setFireDate:[NSDate distantFuture]];

}

- (void)invalid {

if (self.isValid) {

[self invalidate];

}

}

@end

相关文章

  • OC 定时器的内存分析

    定时器用到项目上会有互相引用问题:直接上代码 #import@interface Person : NSObjec...

  • OC-内存管理(一)-定时器NSTimer NSProxy消息转

    OC-内存管理(一)-定时器NSTimer NSProxy消息转发 NSTimer NSTimer会对target...

  • OC底层原理汇总

    OC底层原理(一).alloc实际调用流程分析OC底层原理(二).内存分配与内存对齐OC底层原理(三)、isa、对...

  • Objective-C 对象探究

    本文将分析 OC 对象的本质,对象的内存布局,已经如何为对象分配内存。分析的源码来自 objc-812[https...

  • 结构体内存对齐分析

    这里我们来分析结构体内存对齐方式, 从中我们可以间接的分析出在OC中内存的对齐方式大概什么样. 进行分析前我们要先...

  • iOS 内存对齐探究

    影响OC对象内存大小的因素 数据类型内存大小: 代码分析 通过class_getInstanceSize获取实例的...

  • 通过内存对齐分析IOS中的对象内存占用

    总所周知,oc对象底层是由结构体实现的,所以通过分析结构体内存占用情况可以更好的理解oc对象的内存占用。 1.把O...

  • 定时器启示录

    早些时候看过一些分析定时器内存方面的文章,但在遇到这个bug前我是不屑的。不就是定时器强引用ViewControl...

  • OC对象分析-内存对齐

    第一个内容:对象的内存对齐创建了一个Person类,代码如下: Main函数里打下断点,在alloc后打印下per...

  • OC 资料总结

    OC内存划分OC数据类型

网友评论

      本文标题:OC 定时器的内存分析

      本文链接:https://www.haomeiwen.com/subject/fjzrhxtx.html