Login flow failing after upgrading to iOS9

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-22 08:23:53

问题


After upgrading my app to iOS9 I’am getting an error in my app which says:

: objc[344]: Cannot form weak reference to instance (0x15919e00) of class LoginVC. It is possible that this object was over-released, or is in the process of deallocation.

Below is the function in which i get this error:

-(void)dismissLogin {
self.isLoggingIn = NO;
[self stopLoginAnimation];
[self dismissViewControllerAnimated:YES completion:NO];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self.appDelegate setLoginVC:nil];
[self.view removeFromSuperview];
//[APPDEL selectTabBar];
}

The app gets stuck at the login screen and doesn't switch to next screens.

This error doesn’t come in iOS8. Can anyone help me with this issue.


回答1:


Make sure you are not using instance being deallocated.

I have the same issue. It was not occurring in iOS 8 but occurred in iOS 9. Because I was overriding setDelegate method like this.

-(void)setDelegate:(id<UICollectionViewDelegate>)delegate{
    _internalDelegate = delegate;
    [super setDelegate:self];
}

So in iOS 9, OS sets delegate to nil on de-allocation, but I was setting it to self. So quick fix was

-(void)setDelegate:(id<UICollectionViewDelegate>)delegate{
    _internalDelegate = delegate;
    if (delegate) {
        //Set delegate to self only if original delegate is not nil
        [super setDelegate:self];
    }else{
        [super setDelegate:delegate];
    }
}



回答2:


I ran into this issue recently and this helped me come to the conclusion that I did. The only issue I have with the solution provided above is that if you need the subclass to gain functionality even when its internalDelegate is nil, it just won't work.

Here's the solution I came up with that both prevents the crash and allows functionality to exist even with a nil internalDelegate. Figured I'd share in case anyone else came across this.

  1. Create a second internal property, I called this weakSelf
@property (nonatomic, weak) LoginVC *weakSelf;
  1. Inside any initialization methods, set weakSelf to self
 - (id)init {
    if ((self = [super init])) {
        self.weakSelf = self;
    }
}
  1. Update delegate method
- (void)setDelegate:(id)delegate {
    _internalDelegate = delegate;
    [super setDelegate:self.weakSelf];
}


来源:https://stackoverflow.com/questions/32842995/login-flow-failing-after-upgrading-to-ios9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!