How does a (nonatomic, strong) property's lifespan work in iOS?

孤人 提交于 2019-12-06 09:39:12

问题


Say I have a property declared as : @property (nonatomic, strong) NSArray *menuArr; OR @property (strong) NSArray *menuArr; and set this property in viewDidLoad. How long will the device "remember" the information I have stored in the array?

The property is declared and set in a viewController that is embedded in a navigationViewController that is itself the first view controller in a TabBarViewController. In other words its the first view the user sees then they may navigate away from it and back.

Without getting into a debate over atomic vs nonatomic my question is this

Does a property (declared either way) lives on infinitely in the iOS environment or is its lifespan limited by factors such as time, memory usage elsewhere, turning off the device, etc


To avoid this being an "x y problem" here's why Im asking:

Im working on an app that includes a menu broken up into multiple categories with several items in each category .....as you might expect a menu to be. All of the menu items are stored on parse.com. At first I was doing a separate PFQuery on each page, one on the categories page to get the categories, when user selects a category a new page is pushed and a second PFQuery got all the items in the chosen category. This worked but the pages took quite a while to load, probably 10-15 seconds sometimes with no real indication that the app hadnt just frozen up.

To fix this I decided to run one PFQuery when the first view of the app is loaded in viewDidLoad getting all of the menu items and sorting the myself into nested arrays of categories containing items. I then store the menu array in a property on the viewController. Later, when I go to the menu I have the below in it's viewDidLoad:

//get e reference to the first view controller, the one that has the menu array 
FirstViewController *myVC1ref = (FirstViewController *)[[[self.navigationController.tabBarController.viewControllers objectAtIndex:0] viewControllers]  objectAtIndex:0];

//set thisviewController's `menuArr` property to point to the menuArr on the first viewController.

_menuArr=myVC1ref.menuArr;

My understanding is that this creates a pointer to the original array and does not actually create a second array (please correct me if Im wrong).

This method takes about 10-15 seconds to load and sort the array that one time but then navigation between pages is instant after that which is much better.

I plan to do queries in places to see if any menu items have been changed and if so re-download and sort the menu.

So far in my testing the app seems to remember the info in the array just fine throughout the day with normal unrelated phone usage but there has to be some limits to that right?


回答1:


Your app's memory space will remain valid as long as your app is running. The system is not going to arbitrarily free memory out from under you. How could your app possibly function like that? The type and attributes of the property have absolutely no relevance at this level.

A low memory warning from the system is a request that you manually free up memory that you don't need. The app's memory will either be completely as you left it, or, after moving to the background and then being terminated, a blank slate. If you have data that needs to survive your program exiting, you need to make provisions to save it to disk and read it back.

iOS does have some tricks to preserve and restore your app's state, but that still only applies to a terminated application.



来源:https://stackoverflow.com/questions/23977765/how-does-a-nonatomic-strong-propertys-lifespan-work-in-ios

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