为了更好的活着iOS开发

关于项目中崩溃问题处理:Can't add self as su

2019-02-13  本文已影响0人  MoonLight17

项目中bugly总是收集到Can't add self as subview 的崩溃
错误,崩溃调用堆栈解析如下:

图: 3D520C11-0552-41A6-8FB1-442E0AEC48C1.png

通过分析崩溃堆栈日志解析和崩溃信息提示,只能得出两个线索:
1.addSubView的参数是自己本身self;
2.崩溃和navigationController转场动画有关。

排查

1.addSubView的参数是自己本身self

在工程中用 [self addSubView:self] 测试,确实会崩溃,崩溃的堆栈信息如下。和上传的崩溃信息不一致,可以排除。


屏幕快照 2019-02-12 18.05.08.png

2.navigationController转场动画

navigationController push或者pop上一次操作还没有完成就开始执行下一次操作,同步执行了多个转场操作。如下代码:

    [self.navigationController pushViewController:[[SecondAViewController alloc] init] animated:NO];
    [self.navigationController pushViewController:[[SecondBViewController alloc] init] animated:YES];
    [self.navigationController pushViewController:[[SecondCViewController alloc] init] animated:YES];

同时执行完以上操作,之后的pop退场操作就会导致崩溃。崩溃信息如下:


屏幕快照 2019-02-13 16.37.35.png

push SecondCViewController,C成功入栈,但是视图没有加载到容器中,实际显示的还是B的vc与view,但是栈顶是C的vc。

第一次点击返回时,实际是C出栈,但是当前显示的视图B的view被先加载到formAnimateView与toAnimateView上,原本视图在出栈后应该被释放,但是现在容器栈内还存在B的vc;
当第二次点击返回时,实际应该是B的vc出栈,但是A的view加载到toAnimateView上之后,toAnimateView需要加载到wrapperView进行transition动画, 但wrapperView通过栈顶元素view.superview取值, 而栈顶元素B的view由于上一次错误的转场, 并未在transition动画完成后挂载到wrapperView, 还保留在的临时的动画视图toAnimateView上, 所以使toAnimateView加载到WrapperView的操作变成了动画视图toAnimateView加载到自己上

解决方法

导致转场异常的根本原因是上一次操作还没执行结束就开始执行下一次操作, 同步执行了多个转场操作,这时就需要拦截控制器入栈\出栈的方法,确保当有控制器正在进行入栈\出栈的操作时,没有其他入栈\出栈操作。

实现

通过Runtime的方法魔法Method Swizzling技术实现。分类实现修改navigationControlle的pop和push方法。在push\pop方法中设置一个标志位,动画结束之后,重置标志位,通过标志位来判断push\pop操作是否执行。代码实现如下:

@interface UINavigationController () <UINavigationControllerDelegate>

@property (nonatomic, assign) BOOL viewTransitionInProgress;

@end

@implementation UINavigationController (SafeTransition)

+ (void)load {
    method_exchangeImplementations(class_getInstanceMethod(self, @selector(pushViewController:animated:)),
                                   class_getInstanceMethod(self, @selector(safePushViewController:animated:)));
    
    method_exchangeImplementations(class_getInstanceMethod(self, @selector(popViewControllerAnimated:)),
                                   class_getInstanceMethod(self, @selector(safePopViewControllerAnimated:)));
    
    method_exchangeImplementations(class_getInstanceMethod(self, @selector(popToRootViewControllerAnimated:)),
                                   class_getInstanceMethod(self, @selector(safePopToRootViewControllerAnimated:)));
    
    method_exchangeImplementations(class_getInstanceMethod(self, @selector(popToViewController:animated:)),
                                   class_getInstanceMethod(self, @selector(safePopToViewController:animated:)));
    
}

#pragma mark - setter & getter
- (void)setViewTransitionInProgress:(BOOL)property {
    NSNumber *number = [NSNumber numberWithBool:property];
    objc_setAssociatedObject(self, @selector(viewTransitionInProgress), number, OBJC_ASSOCIATION_RETAIN);
}

- (BOOL)viewTransitionInProgress {
    NSNumber *number = objc_getAssociatedObject(self, @selector(viewTransitionInProgress));
    return [number boolValue];
}

#pragma mark - Intercept Pop, Push, PopToRootVC
- (NSArray *)safePopToRootViewControllerAnimated:(BOOL)animated {
    if (self.viewTransitionInProgress) return nil;
    
    if (animated) {
        self.viewTransitionInProgress = YES;
    }
    
    NSArray *viewControllers = [self safePopToRootViewControllerAnimated:animated];
    if (viewControllers.count == 0) {
        self.viewTransitionInProgress = NO;
    }
    
    return viewControllers;
}

- (NSArray *)safePopToViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.viewTransitionInProgress) return nil;
    
    if (animated){
        self.viewTransitionInProgress = YES;
    }
    
    NSArray *viewControllers = [self safePopToViewController:viewController animated:animated];
    if (viewControllers.count == 0) {
        self.viewTransitionInProgress = NO;
    }
    
    return viewControllers;
}

- (UIViewController *)safePopViewControllerAnimated:(BOOL)animated {
    if (self.viewTransitionInProgress) return nil;
    
    if (animated) {
        self.viewTransitionInProgress = YES;
    }
    
    UIViewController *viewController = [self safePopViewControllerAnimated:animated];
    if (viewController == nil) {
        self.viewTransitionInProgress = NO;
    }
    
    return viewController;
}

- (void)safePushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.viewTransitionInProgress == NO) {
        [self safePushViewController:viewController animated:animated];
        
        if (animated) {
            self.viewTransitionInProgress = YES;
        }
    }
}

@end

@implementation UIViewController (SafeTransitionLock)

+ (void)load {
    Method m1;
    Method m2;
    
    m1 = class_getInstanceMethod(self, @selector(safeViewDidAppear:));
    m2 = class_getInstanceMethod(self, @selector(viewDidAppear:));
    
    method_exchangeImplementations(m1, m2);
}

- (void)safeViewDidAppear:(BOOL)animated {
    self.navigationController.viewTransitionInProgress = NO;
    
    [self safeViewDidAppear:animated];
}

@end

上一篇下一篇

猜你喜欢

热点阅读