我有一个FakeSplashController;
某些东西阻止了此ViewController的释放,并且未调用deinit函数。
此外,AppInitService具有静态函数,并且在此SplashController中被调用。我也尝试将[weak self]
添加到网络请求中。但是,它也不能解决问题。
class SplashViewController: UIViewController {
let logoImage: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "logo")
imageView.contentMode = .scaleAspectFit
return imageView
}()
let textLogo: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "text-logo")
imageView.contentMode = .scaleAspectFit
return imageView
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setupUI()
networkRequests()
}
func networkRequests(){
AppInitService().initAppRequest { [](result) in
switch result{
case .success(_):
self.startAnimation()
return
case .error(let error):
UIControlUtil.showErrorMessage(title: error.title, message: error.message, closeButton: true)
return
}
}
}
func openApp(){
let loginController = WelcomeViewController()
guard let window = UIApplication.shared.keyWindow else {
return
}
window.rootViewController = loginController
}
func startAnimation(){
UIView.animate(withDuration: 0.8, animations: {
self.logoImage.frame.origin.x -= 100
}, completion: nil)
UIView.animate(withDuration: 1,delay: 0.3,animations: {
self.textLogo.alpha = 1
self.textLogo.frame.origin.x += 50
}, completion: { _ in
self.openApp()
})
}
deinit {
print("Splash Deinited")
}
func setupUI(){
self.view.backgroundColor = Color.NavigationBar.tintColor
logoImage.frame = CGRect(x: 0, y: 0, width: 80, height: 80)
logoImage.center = self.view.center
self.view.addSubview(logoImage)
textLogo.frame = CGRect(x: 0,y: 0, width: 195, height: 80)
textLogo.center = self.view.center
textLogo.frame.origin.x -= 20
self.view.addSubview(textLogo)
textLogo.alpha = 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:1)
您正在此块中捕获self
...
func networkRequests(){
AppInitService().initAppRequest { result in
switch result{
case .success(_):
self.startAnimation() // HERE BE DRAGONS!
return
case .error(let error):
UIControlUtil.showErrorMessage(title: error.title, message: error.message, closeButton: true)
return
}
}
}
这是导致内存泄漏的潜在原因。
可以肯定的是,将其更新为...不会捕获自己。
func networkRequests(){
AppInitService().initAppRequest { [weak self] (result) in
switch result{
case .success(_):
self?.startAnimation()
return
case .error(let error):
UIControlUtil.showErrorMessage(title: error.title, message: error.message, closeButton: true)
return
}
}
}
答案 1 :(得分:0)
您可以首先获取数组中的所有视图控制器,然后在检查了相应的视图控制器类之后,可以删除所需的视图控制器。
这是一小段代码:
var navArray:Array = (self.navigationController?.viewControllers)!
navArray.removeAtIndex(0)
self.navigationController?.viewControllers = navArray
我认为这会使您的工作更轻松。
答案 2 :(得分:0)
问题是我在AppDelegate内部将SplashController创建为全局变量和可选变量;
var splashController: SplashController?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
splashController = SplashController()
self.window?.rootViewController = splashController
return true
}
我将其设置为:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let splashController = SplasController()
self.window?.rootViewController = splashController
return true
}
答案 3 :(得分:0)
如果不使用弱引用,则无法释放自身对象,因此此函数会生成可使用弱引用的保留周期瞬间。
// You can use weak reference certain to not capture self by updating it to...
func networkRequests(){
AppInitService().initAppRequest { [weak self] result in
guard let strongSelf = self else {
return
}
switch result{
case .success(_):
strongSelf.startAnimation() // HERE BE DRAGONS!
return
case .error(let error):
UIControlUtil.showErrorMessage(title: error.title, message: error.message, closeButton: true)
return
}
}
}