UIViewController's didReceiveMemoryWarning in ARC environment

情到浓时终转凉″ 提交于 2019-12-23 07:03:47

问题


I'm evaluating the switch to ARC (automatic reference counting) and the refactoring to apply to my code. Among the things I have to figure out, there is this:

what should I do in didReceiveMemoryWarning if the explicit release of objects is not allowed by ARC? Currently, I use that method to release objects belonging to my controller and that are easily retrievable via lazy getters:

- (void)didReceiveMemoryWarning {
    [_foo release]; _foo = nil;
    [super didReceiveMemoryWarning];
}

and the relative lazy getter:

- (Foo *)foo {
    if (_foo) {
        return _foo;
    }
    return (_foo = [[Foo alloc] init]);
}

It seems impossible to implement such "pattern" in ARC… so, what should I do? Should didReceiveMemoryWarning be considered "deprecated"/useless in ARC?


回答1:


ARC handles the retain and release code so setting _foo to be nil is sufficient to allow the ARC generated code to perform the release. You don't explicitly release, you simply manage your object graph and the ARC generated code will perform the release when appropriate.

Read Apple's Transitioning To ARC Release Notes document for more information.




回答2:


You can do several things, but all are app-specific. Things like clearing arrays and dictionaries and setting object references to nil (ARC's way of deleting objects).



来源:https://stackoverflow.com/questions/7766581/uiviewcontrollers-didreceivememorywarning-in-arc-environment

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