如何在Swift中实现didReceiveMemoryWarning? [英] How to implement didReceiveMemoryWarning in Swift?

查看:228
本文介绍了如何在Swift中实现didReceiveMemoryWarning?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我创建一个新的View Controller子类时,Xcode会自动添加方法

Whenever I create a new View Controller subclass, Xcode automatically adds the method

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated
}

通常我只是删除它或忽略它。这就是我见过的所有教程。但我认为,因为Xcode每次都给我,所以它应该有点重要,对吧?我该怎么办?我假设处理资源意味着将它们设置为零,但究竟什么是可以重新创建的资源?

Usually I just delete it or ignore it. This is what all the tutorials I have seen do, too. But I assume that since Xcode gives it to me every time, it should be somewhat important, right? What should I be doing here? I assume that disposing of resources means setting them to nil, but what exactly are "resources that can be recreated"?

我已经看到了这些问题:

I have seen these questions:

  • How to implement didReceiveMemoryWarning?
  • UIViewController's didReceiveMemoryWarning in ARC environment
  • iPhone Development - Simulate Memory Warning

但他们都是斯威夫特之前。虽然我对Objective-C了解不多,但我听说内存管理不同。这对我在 didReceiveMemoryWarning 中应该做什么有什么影响?

But they are all pre-Swift. Although I don't know much about Objective-C, I have heard that the memory management is different. How does that affect what I should do in didReceiveMemoryWarning?

其他说明:


  • 我模糊地了解自动引用计数和延迟实例化

  • 上文档 didReceiveMemoryWarning 我发现相当简短。

  • I am fuzzily aware of Automatic Reference Counting and lazy instantiation
  • The documentation on didReceiveMemoryWarning that I found was rather brief.

推荐答案

Swift



夫特使用ARC就像Objective-C的不( Apple Docs的来源)。同样的规则适用于释放内存,删除对象的所有引用,它将被解除分配。

Swift

Swift uses ARC just like Objective-C does (source to Apple Docs). The same kind of rules apply for freeing memory, remove all of the references to an object and it will be deallocated.


我认为处理资源意味着将它们设置为nil,但究竟什么是可以重新创建的资源?

I assume that disposing of resources means setting them to nil, but what exactly are "resources that can be recreated"?

可以重新创建的资源实际上取决于您的应用程序。

"resources that can be recreated" really depends on your application.

假设您是一款处理大量图片的社交媒体应用。您需要一个快速的用户界面,以便在内存中缓存接下来的20张图片以快速滚动。这些图像始终保存在本地文件系统中。

Say you are a social media app that deals with a lot of pictures. You want a snappy user interface so you cache the next 20 pictures in memory to make scrolling fast. Those images are always saved on the local file system.


  • 图像占用大量内存

  • 你不需要内存中的那些图像。如果应用内存不足,花一点时间从文件加载图像就可以了。

  • 当你收到内存警告时,你可以完全转储图像缓存。

  • 这将释放系统所需的内存

  • Images can take up a lot of memory
  • You don't need those images in memory. If the app is low on memory, taking an extra second to load the image from a file is fine.
  • You could entirely dump the image cache when you receive that memory warning.
  • This will free up memory that the system needs

你正在创造一个有数字的神奇游戏不同程度的。
在您的花式游戏引擎中加载一个级别需要一段时间,所以如果用户有足够的内存,你可以在他们玩2级时加载3级。

You are creating an amazing game that has a number of different levels. Loading a level into your fancy game engine takes a while so if the user has enough memory you can load level 3 while they are playing level 2.


  • 级别占用大量内存

  • 你不需要内存中的下一级别。他们很高兴但不是必不可少的。

  • LevelCache.sharedCache()。nextLevel = nil 释放所有内存

  • Levels take up a lot of memory
  • You don't NEED the next level in memory. They are nice to have but not essential.
  • LevelCache.sharedCache().nextLevel = nil frees up all that memory

永远不要释放屏幕上的东西。我已经看到相关问题的一些答案解除了UIViewController的视图。如果你从屏幕上移除所有东西,你可能会崩溃(在我看来)。

Never deallocate the stuff that is on screen. I've seen some answers to related questions deallocate the view of the UIViewController. If you remove everything from the screen you might at well crash (in my opinion).

如果用户打开了一个他们正在编辑的文档,请不要取消分配。如果您的应用删除了他们的工作而没有得到保存,用户将非常生气。 (事实上​​,你可能应该有一些紧急保存机制来发生这种情况)

If the user has a document open that they are editing, DON'T deallocate it. Users will get exceptional angry at you if your app deletes their work without ever being saved. (In fact, you should probably have some emergency save mechanism for when that happens)

如果你的用户正在为你神话般的社交媒体应用写一篇文章,不要让它他们的工作浪费了。保存并尝试在再次打开应用程序时恢复它。虽然设置我需要做很多工作但我喜欢这样做的应用程序。

If your user is writing a post for your fabulous social media app, don't let their work go to waste. Save it and try to restore it when they open the app again. Although it's a lot of work to setup I love apps that do this.

大多数现代设备很少内存耗尽。该系统可以很好地在后台杀死应用程序,为前台运行的应用程序释放内存。
您可能已经在应用程序切换器中看到了一个应用程序打开但是当您点击应用程序时它已打开到其初始状态。操作系统在后台杀死了应用程序以释放内存。请参见状态恢复,获取信息如何避免这个问题。

Most modern devices rarely run out of memory. The system does a pretty good job of killing apps in the background to free up memory for the app running in the foreground. You have probably seen an app "open" in the App Switcher yet when you tapped on the app it opened to its initial state. The OS killed the app in the background to free up memory. See State Restoration for info on how to avoid this problem.

如果您的应用程序在没有进行大量处理时获得一致的内存警告,请确保您没有先泄漏内存。
检测内存泄漏超出了本答案的范围。 文档教程

If your app is getting consistent memory warnings when you aren't doing a huge amount of processing make sure that you aren't leaking memory first. Detecting memory leaks is outside the scope of this answer. Docs and a tutorial.

这篇关于如何在Swift中实现didReceiveMemoryWarning?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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