美文网首页
发现FlutterViewController的dealloc方

发现FlutterViewController的dealloc方

作者: CodingTom | 来源:发表于2020-04-25 19:51 被阅读0次

    用的Flutter是1.9。

    发现FlutterViewController的dealloc方法不执行。

    经查是注册channel的原因,引起FlutterViewController不释放。

    参考:

    https://www.jianshu.com/p/1173906e73b2

    https://github.com/flutter/flutter/issues/26007

    FlutterBinaryMessenger is strongly referenced in FlutterMethodChannel. If the FlutterBinaryMessenger is a FlutterViewController, then this channel should be strongly referenced so that it can work, as the result the FlutterViewController is strongly referenced too. There is no callback for the controller finally releasing so that I can remove this channel.
    
    下面的self被强引用了,造成了循环引用。
    
    [FlutterMethodChannel
                        methodChannelWithName:commonChannelName
                        binaryMessenger:self];
    
    //如查设成nil的话,就可以释放。但是不可以设nil。
    [FlutterMethodChannel
                        methodChannelWithName:commonChannelName
                        binaryMessenger:nil];
    
    

    因为NSTimer也是强引用target产生循环引用,所以想到用解决NSTimer循环引用的方式解决。

    参考:

    https://www.jianshu.com/p/927449ff3117

    解决方法如下:

    使用:

    
    NSString *commonChannelName = @"com.test/commmon_param";
    FlutterMethodChannel *commonChannel = [FlutterMethodChannel
                                               methodChannelWithName:commonChannelName
                                               binaryMessenger:[WeakProxy proxyWithTarget:self]];
                                               
    
    WeakProxy.h
    
    @interface WeakProxy : NSProxy
    
    @property (weak,nonatomic,readonly)id target;
    
    + (instancetype)proxyWithTarget:(id)target;
    
    - (instancetype)initWithTarget:(id)target;
    
    @end
    
     
    
    WeakProxy.m
    
    #import "WeakProxy.h"
    
    @implementation WeakProxy
    
    - (instancetype)initWithTarget:(id)target{
    
        _target = target;
    
        return self;
    
    }
    
    + (instancetype)proxyWithTarget:(id)target{
    
        return [[self alloc] initWithTarget:target];
    
    }
    
    - (void)forwardInvocation:(NSInvocation *)invocation{
    
        SEL sel = [invocation selector];
    
        if ([self.target respondsToSelector:sel]) {
    
            [invocation invokeWithTarget:self.target];
    
        }
    
    }
    
    - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{
    
        return [self.target methodSignatureForSelector:aSelector];
    
    }
    
    - (BOOL)respondsToSelector:(SEL)aSelector{
    
        return [self.target respondsToSelector:aSelector];
    
    }
    
    @end
    ————————————————
    
    

    原文链接:https://blog.csdn.net/gaoyp/java/article/details/103580750

    相关文章

      网友评论

          本文标题:发现FlutterViewController的dealloc方

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