幼敏悟过人,读书辄成诵。这篇文章主要讲述APP进入后台后才会释放内存相关的知识,希望能为你提供帮助。
【APP进入后台后才会释放内存】我正在开发一个带侧边菜单的应用程序,在ContainController中使用两个ViewController。当关闭侧面菜单ViewController(调用deinit方法)后,内存就不会被释放(大概是23 MiB),直到应用程序进入后台。在Xcode 9中构建的“Leaks”工具表示根本没有内存泄漏......任何人都可以告诉我什么是错的?非常感谢提前!
import UIKit
import CoreDataenum MenuState {
case Collapsed
case Expanding
case Expanded
}class ContainerViewController: UIViewController {weak var managedObjectContext: NSManagedObjectContext!weak var mainViewController: MasterViewController!weak var menuViewController: MenuViewController?var currentState = MenuState.Collapsed {
didSet {
let shouldShowShadow = currentState != .Collapsed
showShadowForMainViewController(shouldShowShadow: shouldShowShadow)
}
}override func viewDidLoad() {
super.viewDidLoad()mainViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "mainView") as! MasterViewController
view.addSubview(mainViewController.view)DispatchQueue.global(qos: .userInteractive).async {
self.mainViewController.managedObjectContext = self.managedObjectContext
self.addChildViewController(self.mainViewController)
self.mainViewController.didMove(toParentViewController: self)
}let panGestureRecognizer = UIPanGestureRecognizer(target: self, action:#selector(self.handlePanGesture(_:)))
self.mainViewController.view.addGestureRecognizer(panGestureRecognizer)let tapGestureRecognizer = UITapGestureRecognizer(target: self, action:#selector(self.handleTapGesture))
self.mainViewController.view.addGestureRecognizer(tapGestureRecognizer)
}@objc func handlePanGesture(_ recognizer: UIPanGestureRecognizer) {switch(recognizer.state) {case .began:let dragFromLeftToRight = (recognizer.velocity(in: view).x >
0)
if (currentState == .Collapsed &
&
dragFromLeftToRight) {
currentState = .Expanding
addMenuViewController()
}case .changed:let positionX = recognizer.view!.frame.origin.x + recognizer.translation(in: view).x
recognizer.view!.frame.origin.x = positionX <
0 ? 0 : positionXlet boundX = mainViewController.view.frame.width - menuViewExpandedOffset
if positionX >
0 {
recognizer.view!.frame.origin.x = positionX >
boundX ? boundX : positionX
}
recognizer.setTranslation(.zero, in: view)case .ended:let boundX = mainViewController.view.frame.width - menuViewExpandedOffset
let hasMovedhanHalfway = (recognizer.view!.frame.origin.x >
boundX * 0.3)
animateMainView(shouldExpand: hasMovedhanHalfway)default:
break
}
}@objc func handleTapGesture() {
if currentState == .Expanded {animateMainView(shouldExpand: false)
}
}//ADD MENUVC
func addMenuViewController() {
if (menuViewController == nil) {
menuViewController = UIStoryboard(name: "Main", bundle: nil)
.instantiateViewController(withIdentifier: "menuView")
as? MenuViewControllerview.insertSubview(menuViewController!.view, at: 0)addChildViewController(menuViewController!)
menuViewController!.didMove(toParentViewController: self)
}
}let menuViewExpandedOffset: CGFloat = 480func animateMainView(shouldExpand: Bool) {if (shouldExpand) {currentState = .Expanded
animateMainViewXPosition(targetPosition: mainViewController.view.frame.width -
menuViewExpandedOffset)
}else {
animateMainViewXPosition(targetPosition: 0) { finished inself.currentState = .Collapsedself.menuViewController?.view.removeFromSuperview()
self.menuViewController?.removeFromParentViewController()
self.menuViewController = nil
}
}
}func animateMainViewXPosition(targetPosition: CGFloat,
completion: ((Bool) ->
Void)! = nil) {UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1.0,
initialSpringVelocity: 0, options: .curveEaseOut, animations: {
self.mainViewController.view.frame.origin.x = targetPosition
}, completion: completion)
}func showShadowForMainViewController(shouldShowShadow: Bool) {
if (shouldShowShadow) {
mainViewController.cameraBGimage.layer.shadowOpacity = 0.6
mainViewController.cameraBGimage.layer.shadowOffset = CGSize(width: 0, height: 1)
} else {
mainViewController.cameraBGimage.layer.shadowOpacity = 0.0
}
}}
答案可以说你的应用程序没有任何问题。 ios系统将以其认为最佳的方式管理内存。
如果你的对象应该被取消分配,那么你没事。如果您想更好地了解视图何时被取消分配,您可以随时使用
Allocations
的de Instruments
工具并按应用程序的名称对其进行过滤。推荐阅读
- Android - 压缩位图,然后将其保存到SDCARD的活动结果中
- 如何在chef recipe中包含application.properties文件中的属性
- 安卓材质按钮,文字上方有图标。
- 从MySql DB检索PDF到android(使用Volley)
- Android(我们什么时候使用getIntent()())
- 如何从我的应用程序在MS-Excel或MS-Word - Android中打开excel,doc文件
- 获取选择选择器意图谷歌驱动器文件文件没有谷歌驱动器Api集成Android
- 如何使用Android Intent System在whatsapp上分享最少n人的文本
- 无法在AndroidManifest.xml中添加Intent