别忘了NSTimer会保留其目标对象

[NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)
                                     target:(nonnull id)
                                   selector:(nonnull SEL)
                                   userInfo:(nullable id)
                                    repeats:(BOOL)];

此方法会创建计时器,并将其预先安排在当前运行循环中,会在指定的时间间隔执行任务。

由于计时器会保留其目标对象,所以反复执行任务通常会导致应用程序出问题。

#import "EOCClass.h"

@implementation EOCClass
{
    NSTimer *_pollingTimer;
}

-(instancetype)init
{
    return [super init];
}

-(void)dealloc
{
    [_pollingTimer invalidate];
}

-(void)p_doPoll
{
    
}

-(void)startPolling
{
    _pollingTimer = [NSTimer scheduledTimerWithTimeInterval:5.0
                                                     target:self
                                                   selector:@selector(p_doPoll)
                                                   userInfo:nil
                                                    repeats:YES];
}

-(void)stopPolling
{
    [_pollingTimer invalidate];
    _pollingTimer = nil;
}

@end

这样写就会有问题,因为实例保存了计时器,计时器又保存了实例。外界对象在释放最后一个指向本实例的引用之前,必须先调用stopPolling方法。

用块可以解决这个问题:

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSTimer (EOCBlocksSupport)

typedef void(^EOCBlock)(void);

+(NSTimer *)eoc_scheduledTimerWithInterval:(NSTimeInterval)interval
                                     block:(EOCBlock)block
                                   repeats:(BOOL)repeats;

@end

NS_ASSUME_NONNULL_END
#import "NSTimer+EOCBlocksSupport.h"

@implementation NSTimer (EOCBlocksSupport)

+(void)eoc_blockInvoke:(NSTimer *)timer
{
    EOCBlock block = timer.userInfo;
    if (block)
    {
        block();
    }
}

+(NSTimer *)eoc_scheduledTimerWithInterval:(NSTimeInterval)interval
                                     block:(EOCBlock)block
                                   repeats:(BOOL)repeats
{
    return [self scheduledTimerWithTimeInterval:interval
                                         target:self
                                       selector:@selector(eoc_blockInvoke:)
                                       userInfo:[block copy]
                                        repeats:repeats];
}

@end
-(void)startPolling
{
    __weak EOCClass *weakSelf = self;
    _pollingTimer = [NSTimer eoc_scheduledTimerWithInterval:5.0
                                                      block:^
    {
        EOCClass *strongSelf = weakSelf;
        [strongSelf p_doPoll];
    }
                                                    repeats:YES];
}

猜你喜欢

转载自blog.csdn.net/run_in_road/article/details/113178055