iPhone-如何处理内存不足的情况 [英] iPhone - How to deal with low memory conditions

查看:127
本文介绍了iPhone-如何处理内存不足的情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当应用收到内存不足警告消息时,可能会发生3种情况:

When the app receives a low memory warning message, 3 situations can happen :

  1. 您的应用刚刚启动,用户没有做任何特殊的事情
  2. 该应用正在运行,并且有当前上下文
  3. 该应用程序在后台运行并且具有一些运行上下文

因此,当您收到此消息时,应该释放您的内存...但是在哪里?以及如何?

So when you receive this message, your are supposed to free memory... But where ? And how ?

我了解:

  1. initWith .....必须设置默认静态值.
  2. viewDidLoad必须加载任何非静态对象
  3. didReceiveMemoryWarning必须释放那些非静态对象
  4. 我不知道在viewDidUnload ...
  5. 中可以/必须做什么.
  1. initWith..... must set the default static values.
  2. viewDidLoad must load any non static object
  3. didReceiveMemoryWarning must free those non static objects
  4. I don't see what can/must be done in viewDidUnload...

我想某些保留值必须在didReceiveMemoryWarning中的某处设置为nil?

I guess some retained values must be set to nil somewhere... in didReceiveMemoryWarning ?

必须使用活动上下文(屏幕上的事物的位置,显示的文本等)进行什么操作,以便当再次调用viewDidLoad时,这些事物再次出现在memoryWarning调用之前的位置?

And what must be done with the active context (positions of thing of the screen, displayed text, ...) so when viewDidLoad is called again, those thing appear again as they where before the memoryWarning call ?

我的意思是,想象两种情况:

I mean, imagine 2 scenarios :

场景1

  1. 您正在做某事...您在字段中写了一些文本,没有保存,打开了另一个视图,在屏幕上移动了一个视图.
  2. 您在后台发送应用.
  3. 然后将内存警告发送到应用程序.
  4. 之后,用户将应用程序发送到前台:它应该显示退出时,当前视图和以前的视图一样的内容,但是如果所有内容都已发布,您该怎么做?

场景2

  1. 您正在做某事...您在一个字段中写了一些文本,没有保存,打开了另一个视图,在屏幕上移动了一个视图.
  2. 然后将内存警告发送到应用程序.
  3. 您不想失去视图上的内容,也不想失去上一个视图上的内容.您不希望两个屏幕都因释放/重新加载功能而闪烁.您如何处理呢?

因此,当发生这些内存警告时,除了将内容写入磁盘以供日后显示时,您还有其他选择吗?

So when those memory warning happens, do you have any other choice than writing things to disk to display them back later ?

什么时候再次加载它们?我有一个viewController加载(viewDidLoad),接收一个memoryWarning,卸载(viewDidUnload),但是回到它时,不再调用viewDidLoad吗?是否必须在viewWillAppear中完成此操作?我是否必须认为在触发viewWillAppear的任何时候,我都可以假定应该在其上显示的内容已加载?

And when do you load those again ? I have a viewController that loads (viewDidLoad), receive a memoryWarning, unloads (viewDidUnload), but when going back to it, viewDidLoad is not called again ? Do this must be done in viewWillAppear ? Do I have to think that anytime viewWillAppear is triggered, I can assume that things that are supposed to be displayed on it are loaded ?

即使有有价值的链接,任何帮助都是很棒的!

Any help, even with valuable links, would be great !

谢谢您的帮助.

推荐答案

考虑方案的替代方案.如果您的应用程序没有释放足够的内存,则可能会被杀死,这对用户而言更是令人讨厌.人们可能会选择使当前显示闪烁,而不是丢失用户的宝贵数据.

Consider the alternatives to your scenarios. Your app could be killed if it does not free enough memory, which would be even more jarring to the user. One might choose potentially flickering the current display over losing the user's valuable data.

这篇关于iPhone-如何处理内存不足的情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆