ARC - popviewController/DismissViewController

久未见 提交于 2019-12-11 07:19:40

问题


I have some doubts with the ARC.

In ARC: When a viewcontroller is dismissed by using the dismissViewController/popViewController, Will it's allocated memory automatically get released?

What will happen when there is an asynchronous NSURLConnection is running and before that operation completed, viewcontroller got dismissed?

Thank you


回答1:


When you dismiss a view controller (or pop it), it will be deallocated if you didn't make any strong pointers to it (that controller is retained by the navigation controller, or the presenting view controller, so you usually don't need to have a pointer to it when you create it and push or present it).

If an NSURLConnection is in progress when you dismiss the controller, and that controller is the delegate of the connection (which is the usual case), then the controller will not be deallocated until connectionDidFinishLoading or connectionDidFailWithError is completed. This is because the connection is still alive, and it has a strong pointer to its delegate.




回答2:


Dismissviewcontroller will stop the view controller from being displayed. The actual view controller will only be removed from memory under ARC when the actual variable referring to the view controller is out of scope. E.g.

ViewControllerType *vc = [[ViewControllerType alloc] init]; // Create it
[self presentViewController:vc animated:TRUE completion:Nil]; // Present it


vc = Nil; // Destroy it, or the method vc was declared inside is out of scope does the same

Typically you wouldn't do vc = Nil while vc is still displayed

If you allow the ViewController to dismiss prior to receiving a response for your NSURLConnection you should deal inside the response thread of the NSURLConnection for the condition that the ViewController is no longer there. Remember, however that the ViewController may still be in scope. It's really a question that can only be answered with a closer look at the use case and associated code.



来源:https://stackoverflow.com/questions/20438908/arc-popviewcontroller-dismissviewcontroller

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