ios - 调用 applicationWillEnterForeground 后重新启动应用程序

标签 ios swift restart

我想从第一个 View 重新启动应用程序,如下所示:

func applicationWillEnterForeground(_ application: UIApplication) {
    let storyBoard: UIStoryboard = UIStoryboard(name: "SplashScreen", bundle: nil)
    let splashScreen = storyBoard.instantiateViewController(withIdentifier: "splashVC") as! SplashScreenController
    self.window?.addSubview(splashScreen.view)

}

但是当应用程序再次被唤醒时。我的代表返回零。为什么?

SplashScreenVM.swift:

  import Foundation
protocol SplashScreenVMProtocol: class {
    func fetchDataSuccessfuly()
    func failedFetchData()
}
class SplashScreenVM {

    weak var delegate: SplashScreenVMProtocol!
    private let dataManager = DataManagement()
    private let coreDataManager = CoreDataManagement()

    lazy var itemsCount = coreDataManager.getRecords("Categories").count
    lazy var timestamp = coreDataManager.getRecords("Timestamp")

    init(){}

    func setUpData() {
        dataManager.fetchData(timestamp: 0 ) { (err, res) in
        //dataManager.fetchData(timestamp: timestamp.count == 0 ? 0 : timestamp[0].value(forKey: "time") as! Int64) { (err, res) in
            //print("categories count: \(self.coreDataManager.getRecords("Categories").count) and artciles count \(self.coreDataManager.getRecords("Articles").count)")

            if(err != nil) {
                print("Error: \(String(describing: err))");
                self.delegate.failedFetchData()
                return
            }
                self.coreDataManager.setTimestamp()
                self.delegate.fetchDataSuccessfuly()
        }
    }
}

打开应用程序时我的第一个 View SplashScreenController:

    import UIKit
import Alamofire

class SplashScreenController: UIViewController, SplashScreenVMProtocol {

    @IBOutlet weak var loadSpinner: UIActivityIndicatorView!

    let splashScreenVM = SplashScreenVM()

    let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    lazy var MainController = storyBoard.instantiateViewController(withIdentifier: "MainVC")

    override func viewDidLoad() {
        super.viewDidLoad()


        splashScreenVM.delegate = self  //Looks like something wrong with this
        print("apapapaap \(splashScreenVM.delegate)")
        loadSpinner.hidesWhenStopped = true
        loadSpinner.startAnimating()

        switch Reachability.isConnectedToNetwork() {
            case true:
                splashScreenVM.setUpData()
            case false:
                self.loadSpinner.stopAnimating()
                splashScreenVM.itemsCount == 0 ? print("butinai reikia intiko") : print("keliaujam i pagr.meniu")
        }
    }

    func fetchDataSuccessfuly() {
        self.loadSpinner.stopAnimating()
        self.present(MainController, animated: true, completion: nil)
    }

    func failedFetchData() {
        self.loadSpinner.stopAnimating()
        if(splashScreenVM.itemsCount != 0) {
            self.present(MainController, animated: true, completion: nil)
        }
        return
    }   
}

我做错了什么?我的弱变量委托(delegate):SplashScreenVMProtocol!返回零。当应用程序再次唤醒时从头开始启动应用程序是更好的方法吗?

最佳答案

您没有在applicationWillEnterForeground中保留splashScreen变量。您将splashScreen View 设置为 subview ,但splashScreen本身只是一个局部变量。 当它超出范围时,该对象将被释放。

您的数据访问是异步发生的,因此当它完成时,原始的splashScreen对象已经被释放并且不再存在。

需要有东西固定在启动屏幕上。

关于ios - 调用 applicationWillEnterForeground 后重新启动应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52315528/

相关文章:

iphone - 如何以编程方式播放默认电话铃声?

ios - 快速从多个 View 获取数据

restart - 如何在 Airflow 1.8 上出现故障时重新启动 dag?

docker - Golang Docker 容器未在 Docker-Compose 中重新启动

ios - Redux:最佳实践(使用 Swift)

ios - 如何停止 UIKeyboard 返回键工作

ios - 在 Swift 3+ 中使用 NSPredicate 对自定义对象进行过滤

ios - 创建一个 Sprite 并给它一个自定义类

ios - 如何多次推送同一个 ViewController?

python - 在 sleep 期间安全地重新启动Python程序?