viewDidUnload
and viewWillUnload
and decided that ultimately the memory hit from releasing objects in these methods wasn't enough of a concern to keep those methods and they were deprecated. It was basically a nice way of saying that a lot of developers are so terrible at view lifecycle management that they were just going to remove the unload methods so that developers didn't continue to mess up their app by not knowing what they were doing. Now I personally applaud Apple for taking into account this problem and dealing with it in a way that doesn't hurt legacy apps. However, I do believe that all APIs are tools that when wielded appropriately can be useful for building something that works well. So when viewDidUnload
and viewWillUnload
were deprecated, one of the first things I set out to do was port them so that I could continue to use these "tools" into the future. Now, if you don't want to use viewDidUnload
or viewWillUnload
and want to conform to Apple's recommended application life cycle design guidelines then this article won't likely help you, and I must credit your wisdom. For the rest of you, let's jump into memory warnings!
viewWillUnload
and viewDidUnload
ported to iOS 6
So to implement the new
viewDidUnload
and viewWillUnload
methods we need to know when they are supposed to be executed. Pretty simply, when a UIViewController
gets a memoryWarning and it's view is not actively visible, it will call viewWillUnload, release its view and then will call viewDidUnload. So let's start by implementing our revised didReceiveMemoryWarning
method.- (void) didReceiveMemoryWarningWithViewUnloading { if (_cmd == @selector(didReceiveMemoryWarning)) { // we were swizzled, call the original [self didReceiveMemoryWarningWithViewUnloading]; } else { // not swizzled and called directly [self didReceiveMemoryWarning]; } static BOOL s_portUnloading = NO; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSArray* comps = [[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."]; s_portUnloading = [[comps objectAtIndex:0] integerValue] >= 6; }); if (s_portUnloading) { if (self.isViewLoaded && !self.view.window) { if ([self respondsToSelector:@selector(viewWillUnload)]) { [self viewWillUnload]; } self.view = nil; // unload NSAssert(!self.isViewLoaded); if ([self respondsToSelector:@selector(viewDidUnload)]) { [self viewDidUnload]; } } } }
First thing we do in our method to support unloading a view controller's view on a memory warning is ensure that the original memory warning method is called. This is as easy as doing a simple selector comparison to see if we are swizzled or not.
Next we need to check that our OS version is iOS 6 or later to ensure we are extending the functionality only where it needs to be extended. We don't however want to take the overhead of making the OS version check over and over again, so we will use a
static BOOL
to store whether we are iOS 6+ or not. Now as far as checking the OS, I'm just parsing the systemVersion
but honestly every app should have a strong mechanism for OS Version checking. I personally have a custom Version
object that is an object representation of a version with an class method for easily accessing the OS Version (as well as another for the Application Version). I should do a post on Version objects in the future as it's really something every application should have.Once we've checked that we do want to support the unloading of the view we just replicate the view unload logic that older iOS versions already support.
- Only unload if the view is loaded AND is not currently visible (I use self.view.superview as the check, however, it could be feasible to check if the view hierarchy exists in a window but we'd also need to remove the view before unloading it so that it's not a dangling view reference inside some other view)
- Call
viewWillUnload
if available - Unload the view
- Call
viewDidUnload
if available
Ok, we've got our replacement method so lets implement the category class method we'll use to turn on view unloading by memory warnings.
@implementation UIViewController (ViewUnloadSupport) + (void) portViewUnloadSupport { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSArray* comps = [[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."]; if ([[comps objectAtIndex:0] integerValue] >= 6) { SwizzleInstanceMethods([UIViewController class], @selector(didReceiveMemoryWarning), @selector(didReceiveMemoryWarningWithViewUnloading)); } }); } @end
Alright, no problem! If the OS is greater than or equal to iOS 6, let's swizzle the
didReceiveMemoryWarning
instance method. All we have to do is call [UIViewController portViewUnloadSupport];
from the application did load method and we have view unloading support once again in iOS 6. NOTE: I did not provide the @interface
declaration of the category but it just seemed like trivial detail.
The innovative thinking of you in this blog. This blog makes me very useful to learn. I like the information. Good work and keep update more.
ReplyDeleteLoadRunner Training in Chennai
QTP Training in Chennai
core java training in chennai
clinical sas training in chennai
javascript training in chennai
Spring Training in Chennai
Hibernate Training in Chennai
Manual Testing Training in Chennai
JMeter Training in Chennai
Big data is a term that describes the large volume of data – both structured and unstructured – that inundates a business on a day-to-day basis. big data projects for students But it’s not the amount of data that’s important.Project Center in Chennai
DeleteSpring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Corporate TRaining Spring Framework the authors explore the idea of using Java in Big Data platforms.
Spring Training in Chennai
The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training
I would definitely thank the admin of this blog for sharing this information with us. Waiting for more updates from this blog admin.
ReplyDeleteRPA Training in Chennai
Robotic Process Automation Certification
Blue Prism Training Institute in Chennai
UiPath Training in Chennai
DevOps Training in Chennai
Data Science Course in Chennai
Robotic Process Automation Training
RPA course in Chennai
RPA course in Chennai
This is a very helpful blog for one who needs to learn in a short span of time.
ReplyDeleteSpoken English Classes in Chennai
Best Spoken English Classes in Chennai
Spoken English Class in Chennai
Spoken English in Chennai
English Speaking Classes in Mumbai
English Speaking Course in Mumbai
IELTS Coaching in Chennai
IELTS Coaching Centre in Chennai
IELTS Classes in Mumbai
IELTS Coaching in Mumbai
Great, this article is quite awesome and I have bookmarked this page for my future reference.
ReplyDeleteWeb Designing Course in chennai
Web development training in chennai
web designing training institute in chennai
AngularJS course in Chennai
PHP Training Institute in Chennai
ccna course in Chennai
Ethical Hacking course in Chennai
Web Designing Course in Anna Nagar
Web Designing Course in Vadapalani
Web Designing Course in Thiruvanmiyur
ReplyDeleteYou have provided a nice article, Thank you very much for this one. And I hope this will be useful for many people. And I am waiting for your next post keep on updating these kinds of knowledgeable things
Android Training in Chennai
Android Course in Chennai
App Development Course in Chennai
Android Development Course in Chennai
Android App Development Course in Chennai
You have provided a nice article, Thank you very much for this one. And I hope this will be useful for many people. And I am waiting for your next post keep on updating these kinds of knowledgeable things
ReplyDeleteApp Development Course in Chennai
Android Development Course in Chennai
Android Training Institutes in Bangalore
Android App Development Course in Bangalore
Android Course in Coimbatore
Android App Development Course in Coimbatore
Android Course in Madurai
KBC Lottery Winner is lucky chance for lottery seekers in India.
ReplyDeleteSuch a very useful article. Very interesting to read this article. I would like to thank you for the efforts you had made for writing this awesome article.
ReplyDeleteData Science Course in Pune
Data Science Training in Pune
Thumbs up guys your doing a really good job. It is the intent to provide valuable information and best practices, including an understanding of the regulatory process.
ReplyDeleteCyber Security Course in Bangalore
After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article.
ReplyDeleteEthical Hacking Course in Bangalore
Very nice blog and articles. I am really very happy to visit your blog. Now I am found which I actually want. I check your blog everyday and try to learn something from your blog. Thank you and waiting for your new post.
ReplyDeleteCyber Security Training in Bangalore
Wow! Such an amazing and helpful post this is. I really really love it. I hope that you continue to do your work like this in the future also.
ReplyDeleteEthical Hacking Training in Bangalore
Great post i must say and thanks for the information.
ReplyDeleteData Science Training Institute in Bangalore
Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
ReplyDeleteBest Data Science Courses in Bangalore
I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job !
ReplyDeleteData Science Course in Bangalore
I’m excited to uncover this page. I need to to thank you for ones time for this particularly fantastic read !! I definitely really liked every part of it and i also have you saved to fav to look at new information in your site.
ReplyDeleteData Science Training in Bangalore
I am impressed by the information that you have on this blog. Thanks for Sharing
ReplyDeleteEthical Hacking in Bangalore
Good to learn something new from this blog. Thanks for sharing such a worthy article.keep it up!!
ReplyDeleteandroid training in chennai
android online training in chennai
android training in bangalore
android training in hyderabad
android Training in coimbatore
android training
android online training
I have to search sites with relevant information ,This is a
ReplyDeletewonderful blog,These type of blog keeps the users interest in
the website, i am impressed. thank you.
Data Science Course in Bangalore
You can comment on the blog ordering system. You should discuss, it's splendid. Auditing your blog would increase the number of visitors. I was very happy to find this site. Thank you...
ReplyDeleteBraces in Bangalore
I read that Post and got it fine and informative. Please share more like that...
ReplyDeletedata science course noida