2

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?

4

1 回答 1

5

只要您的应用程序正在运行,您的应用程序的内存空间就会保持有效。系统不会随意释放您下方的内存。你的应用程序怎么可能这样运行?属性的类型和属性在这个级别上完全没有相关性。

来自系统的内存不足警告是您手动释放不需要的内存的请求。应用程序的内存将在您离开时完全保留,或者在移动到后台然后被终止后,一片空白。如果您有数据需要在程序退出后继续存在,您需要做好准备将其保存到磁盘并读回。

iOS确实有一些技巧来保存和恢复应用程序的状态,但这仍然只适用于终止的应用程序。

于 2014-06-01T08:46:38.723 回答