iOS面试

iOS中Cell或SubView的timer释放问题

2018-03-17  本文已影响8人  饭_米粒

问题

在很多场景下可能需要在cell或view中添加计时器进行倒计时的处理,但是这里会有一个问题就是timer的释放问题。如果没有在controller释放时进行释放,timer会一直被强引用造成内存泄露。

在网上看到了几种解决方案,但是都不是很理想,最终在这篇文章中找到了觉得是最好的方法。但是在这个文章里用到了RAC的开源库,对于大部分人来说可能并不会引用这个库。在这里会通过类别的方式来对最后一种方案来进行改造,使得可以在任何地方使用。

方案分析

在这里简单说明下解决方案。由于在cell中使用了NSTimer作为属性,在这种情况下cell不会自动调用-dealloc方法来释放内存。这样的话我们只能通过在controller释放内存时来释放timer来达到目的。在这里我们会添加两个类别,一个是UIView+ManagerCaller.h类别来获取持有view的controller实例,一个是NSObject+DeallocBlock.h类别来添加调用-dealloc函数时的回调函数。

贴下代码

#import "UIView+ManagerCaller.h"

@implementation UIView (ManagerCaller)

- (UIViewController*)getManagerController
{
    UIResponder* responder = self;
    while (responder && ![responder isKindOfClass:[UIViewController class]]) {
        responder = responder.nextResponder;
    }
    if (!responder) {
        return nil;
    }
    if ([responder isKindOfClass:[UIViewController class]]) {
        return (UIViewController*)responder;
    }
    return nil;
}
@end
#import "NSObject+DeallocBlock.h"
#import <objc/runtime.h>

@interface FDOnDeallocBlockHolder : NSObject

@property (copy, nonatomic) FDObjectDeallocBlock block;
@property (assign, nonatomic) void* key;

@end

@implementation FDOnDeallocBlockHolder

- (instancetype)initWithBlock: (FDObjectDeallocBlock)block
{
    self = [super init];
    self.block = block;
    return self;
}

- (void)dealloc
{
    FDObjectDeallocBlock block = _block;
    _block = nil;
    if (block) {
        block();
    }
}

@end

@implementation NSObject (DeallocBlock)

- (void*)addOnDeallocBlock: (FDObjectDeallocBlock)block forKey: (void*)key
{
    FDOnDeallocBlockHolder* holder = [[FDOnDeallocBlockHolder alloc] initWithBlock:block];
    
    if (!key) {
        key = ((__bridge void*)holder);
    } else {
        holder.key = key;
    }
    
    objc_setAssociatedObject(self, key, holder, OBJC_ASSOCIATION_RETAIN);
    [self cacheOnDeallocBlockHolder:holder];
    return key;
}

- (void*)addOnDeallocBlock: (FDObjectDeallocBlock)block
{
    return [self addOnDeallocBlock:block forKey:nil];
}

- (BOOL)hasOnDeallocBlockWithKey: (void*)key
{
    return objc_getAssociatedObject(self, key) != nil;
}

- (void)removeOnDeallocBlockByKey: (void*)key
{
    NSMutableArray* list = [self onDeallocBlockHolderList];
    for (NSValue* holderReference in list) {
        FDOnDeallocBlockHolder* holder = holderReference.nonretainedObjectValue;
        if (holder && (holder == key || holder.key == key)) {
            holder.block = nil;
            [list removeObject:holderReference];
            break;
        }
    }
    objc_setAssociatedObject(self, key, nil, OBJC_ASSOCIATION_RETAIN);
}

- (void)cacheOnDeallocBlockHolder: (FDOnDeallocBlockHolder*)holder
{
    NSMutableArray* list = [self onDeallocBlockHolderList];
    [list addObject:[NSValue valueWithNonretainedObject:holder]];
}

- (NSMutableArray<NSValue*>*)onDeallocBlockHolderList
{
    NSMutableArray* list = objc_getAssociatedObject(self, @selector(onDeallocBlockHolderList));
    if (!list) {
        list = [[NSMutableArray alloc] init];
        objc_setAssociatedObject(self, @selector(onDeallocBlockHolderList), list, OBJC_ASSOCIATION_RETAIN);
    }
    return list;
}

- (void)executeAllOnDeallocBlocks
{
    NSMutableArray* list = [self onDeallocBlockHolderList];
    for (NSValue* holderReference in list) {
        FDOnDeallocBlockHolder* holder = holderReference.nonretainedObjectValue;
        if (!holder) {
            continue;
        }
        FDObjectDeallocBlock block = holder.block;
        holder.block = nil;
        if (!block) {
            continue;
        }
        void* key = ((__bridge void*)holder);
        objc_setAssociatedObject(self, key, nil, OBJC_ASSOCIATION_RETAIN);
        block();
    }
    [list removeAllObjects];
}
@end

代码分析

效果演示

下面贴两张图,分别是添加回调函数前后Log对比。


添加回调函数前 添加回调函数后

总结

在上面这个问题里,主要解决思路就是怎么让cell或者subview在controller触发-dealloc后执行回调函数,这里涉及到了内存释放机制,我在这里也只是简单应用了而已,可能还有一些情况没有考虑到会有其他问题。后续还是要再研究下内存释放机制的更深层的内容。

Demo地址

参考文章
如何释放含有NSTimer的UITableViewCell(SubView)

上一篇下一篇

猜你喜欢

热点阅读