简体   繁体   English

weakSelf在被阻止之前被解除分配

[英]weakSelf being deallocated before block

So somehow my weakSelf variable is being deallocated before my block has a chance to execute. 所以我的weakSelf变量在我的块有机会执行之前被解除分配。 This only happens in one specific scenario, the other times I hit this block it works fine. 这只发生在一个特定的情况下,其他时候我点击这个块它工作正常。 Here's what my code looks like: 这是我的代码的样子:

__weak typeof(self) weakSelf = self;
DBTEligibleAccountFetcher *accountFetcher = [[DBTEligibleAccountFetcher alloc] init];
NSArray *eligibleDepositAccounts = [accountFetcher fetchDepositEligibleAccounts];
if(eligibleDepositAccounts.count == 1) {
    DBTDepositAmountLimitsHandler *limitChecker = [[DBTDepositAmountLimitsHandler alloc] init];
    [limitChecker handleRequest:self.navigationController bankAccount:eligibleDepositAccounts.firstObject completionBlock:^(DBDepositCheckAccountLimits *limitDetails) {
        containerController.limitDetails = limitDetails;
        [weakSelf.navigationController handleNewRootPush:containerController withCompletion:completionBlock animated:YES];
    }];
} else {
    [self.navigationController handleNewRootPush:containerController withCompletion:completionBlock animated:YES];
}

By the time it gets to the weakSelf.navigationController... line, weakSelf is nil. 当它到达weakSelf.navigationController...行时, weakSelf为零。 I'm not sure how to even debug this or how the variable could get deallocated before it is even used. 我不确定如何调试这个或者变量如何在它被使用之前被释放。

Does anyone know any scenarios that could be going on right now? 有谁知道现在可能发生的任何情况? Or any tips on how I might go about debugging this issue? 或者有关如何调试此问题的任何提示?

Let's say you have a view controller, the user taps a button, therefore you send some information to the server asynchronously and you are prepared to handle when the request comes back. 假设您有一个视图控制器,用户点击一个按钮,因此您可以异步地向服务器发送一些信息,并且您准备在请求返回时进行处理。 Meanwhile your user switches to another view and your view controller gets deallocated before your method returns. 同时,您的用户切换到另一个视图,并在您的方法返回之前取消分配视图控制器。

First consider what you want to happen. 首先考虑一下你想要发生的事情。 Sometimes a request has become pointless if the view controller is gone. 如果视图控制器消失,有时请求变得毫无意义。 Say you have a site that translates German to French. 假设您有一个将德语翻译成法语的网站。 If the translation comes back and your view controller is gone, you just ignore the result. 如果翻译恢复并且您的视图控制器消失了,您只需忽略结果。 But if you have to process the result, whether the view controller is there or not, then you need to rearrange your code so that it works even if the view controller is nil because the user switched to a different view. 但是,如果您必须处理结果,无论视图控制器是否存在,那么您需要重新排列代码,以便即使视图控制器为零也可以工作,因为用户切换到了不同的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM