對(duì)象提前釋放的發(fā)現(xiàn)和處理2019-08-27

  • (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    [self initView];
    SetRootViewControllerHandle *handle = [[SetRootViewControllerHandle alloc] init];
    [handle setRootViewController];
    }
    源代碼是這樣定義的,創(chuàng)建的臨時(shí)變量;在SetRootViewControllerHandle.m文件中的方法實(shí)現(xiàn)如下

  • (void)setRootViewController
    {
    if (self.isLogin) {// 登錄狀態(tài)
    [self.viewModel initNetWorkOfGetUserInfo];

    } else {// 非登錄狀態(tài)
    LoginViewController *VC = [[LoginViewController alloc] init];
    VC.openType = OpenTypeRoot;
    BaseNavigationController *naviC = [[BaseNavigationController alloc] initWithRootViewController:VC];
    AppDelegateInstance.window.rootViewController = naviC;
    }
    }
    在登錄狀態(tài)下,直接網(wǎng)絡(luò)請(qǐng)求數(shù)據(jù)后在進(jìn)行視圖控制器的變換操作,但是SetRootViewControllerHandle是臨時(shí)變量,代碼運(yùn)行后就直接銷毀了,但是網(wǎng)絡(luò)請(qǐng)求是異步操作,就導(dǎo)致網(wǎng)絡(luò)請(qǐng)求的對(duì)象提前釋放,導(dǎo)致的結(jié)果閃退或者無反應(yīng),所以不應(yīng)該定義為臨時(shí)變量

@property (nonatomic,strong) SetRootViewControllerHandle *handle;

  • (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    [self initView];
    self.handle = [[SetRootViewControllerHandle alloc] init];
    [self.handle setRootViewController];
    }
    增加對(duì)象的持有時(shí)間就好了咯
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容