Я хочу перезапустить приложение с первого взгляда так:
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()
}
}
}
Мой первый просмотр при открытии приложения 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
}
}
Что я делаю не так?Мой слабый делегат var: SplashScreenVMProtocol!возвращает нольЭто лучший способ начать приложение с самого начала, когда приложение снова проснулся?