Thursday, February 28, 2013

View Unloading into the Future!

When Apple released iOS 6, they stated that developers were having "trouble" with properly using 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.

  1. 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)
  2. Call viewWillUnload if available
  3. Unload the view
  4. 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.

62 comments:

  1. Replies
    1. 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

      Spring 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

      Delete

  2. 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
    Android Training in Chennai
    Android Course in Chennai
    App Development Course in Chennai
    Android Development Course in Chennai
    Android App Development Course in Chennai

    ReplyDelete
  3. 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
    App 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

    ReplyDelete
  4. 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.
    Data Science Course in Pune
    Data Science Training in Pune

    ReplyDelete
  5. 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.
    Cyber Security Course in Bangalore

    ReplyDelete
  6. 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.
    Ethical Hacking Course in Bangalore

    ReplyDelete
  7. 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.
    Cyber Security Training in Bangalore

    ReplyDelete
  8. 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.
    Ethical Hacking Training in Bangalore

    ReplyDelete
  9. 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.
    Best Data Science Courses in Bangalore

    ReplyDelete
  10. 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 !
    Data Science Course in Bangalore

    ReplyDelete
  11. 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.
    Data Science Training in Bangalore

    ReplyDelete
  12. I am impressed by the information that you have on this blog. Thanks for Sharing
    Ethical Hacking in Bangalore

    ReplyDelete
  13. I have to search sites with relevant information ,This is a
    wonderful blog,These type of blog keeps the users interest in
    the website, i am impressed. thank you.
    Data Science Course in Bangalore

    ReplyDelete
  14. 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...
    Braces in Bangalore

    ReplyDelete
  15. I read that Post and got it fine and informative. Please share more like that...
    data science course noida

    ReplyDelete
  16. You totally coordinate our desire and the assortment of our data.
    data science course

    ReplyDelete
  17. i am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
    data science courses in hyderabad

    ReplyDelete
  18. First You got a great blog .I will be interested in more similar topics. i see you got really very useful topics, i will be always checking your blog thanks.
    data science training in malaysia

    ReplyDelete
  19. I am impressed by the information that you have on this blog. It shows how well you understand this subject.
    data science training in malaysia

    ReplyDelete
  20. Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you Best web application development company service provider.

    ReplyDelete
  21. Extremely overall quite fascinating post. I was searching for this sort of data and delighted in perusing this one. Continue posting. A debt of gratitude is in order for sharing. python course in delhi

    ReplyDelete

  22. I was basically inspecting through the web filtering for certain data and ran over your blog. I am flabbergasted by the data that you have on this blog. It shows how well you welcome this subject. Bookmarked this page, will return for extra. data science course in jaipur

    ReplyDelete
  23. I love your post and what you share with us is updated and quite informative, I would like to bookmark the page so that I can come here again for you to read, because you have done an amazing job. Turkey Visa for US Citizens, an Online Visa. An online visa system has been introduced that allows citizens to obtain a visit visa electronically.

    ReplyDelete
  24. Very good message. I came across your blog and wanted to tell you that I really enjoyed reading your articles.

    Data Analytics Course in Bangalore

    ReplyDelete
  25. It's like you've got the point right, but forgot to include your readers. Maybe you should think about it from different angles.

    Digital Marketing Course in Bangalore

    ReplyDelete
  26. Very good message. I came across your blog and wanted to tell you that I really enjoyed reading your articles.

    Business Analytics Course

    ReplyDelete
  27. Very good message. I came across your blog and wanted to tell you that I really enjoyed reading your articles.


    Best Data Science Courses in Bangalore

    ReplyDelete
  28. This is really an informative articles. Do you have any idea about that Kenya evisa cost. Now we can easily know about that within few seconds

    ReplyDelete

  29. Thanks for the informative and helpful post, obviously in your blog everything is good..
    data scientist course in malaysia

    ReplyDelete
  30. Impressive. Your story always bring hope and new energy. Keep up the good work.
    data scientist certification malaysia

    ReplyDelete
  31. This is a very useful article. Indian emergency visa application form, you can get a visa within 1 to 3 working days by filling the India emergency visa application form.

    ReplyDelete

  32. Interesting and attractive information. This blog is really rocking... Yes, the post is very interesting and I enjoy it a lot. How India Visa Works? In 3 simple steps you can get your visa. You can fill your online application form &, then upload all documents & pay your visa fee.

    ReplyDelete
  33. İnstagram takipçi satın al! İnstagram takipçi sitesi ile takipçi satın al sende sosyal medyada fenomen olmaya bir adım at. Sende hemen instagram takipçi satın almak istiyorsan tıkla:

    1- takipçi satın al

    2- takipçi satın al

    3- takipçi satın al

    ReplyDelete
  34. Thanks for sharing this great article we appreciate it, we provide instagram reels download freely and unlimited.

    ReplyDelete

  35. Hey friend, You are interested in travel to India from USA, Indian e-Visa is issued for USA citizens under the following 5 categories. More info about Indian visas you can read through our website.

    ReplyDelete
  36. Hii sir, Thanks for sharing a great blog!! Your writing material is very impressive. Many people ask, can we get Turkey transit visa? Yes, indeed. You can get a Turkey transit visa online.In fact, it is a very quick, cheap and easy way to get a Turkey visa.

    ReplyDelete
  37. you can surely peruse and modify anything you compose with Grammarly's free composing application. Grammarly for windows customers may be completely tested by using Grammarly for gifted clients by using coordinating it. Grammarly Full Version Crack

    ReplyDelete
  38. The wi-fi safety consultant component, protect the consumer from intimidation that happen from wireless relations specially, the application check such wireless network linked to your computer and give alerts of their safety rank. Bitdefender Total Security 2019 Activation Code Latest

    ReplyDelete
  39. May the bond of love that we share is always blessed by the Almighty. Wishing a blessed Christmas to you. https://wishesquotz.com/christmas-wishes-for-loved-ones/

    ReplyDelete