i want to restart app from first view like that:
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)
}
But when app is waked up again. My delegate return nil. Why?
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()
}
}
}
My first view when app is opened 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
}
}
What i'm doing wrong? My weak var delegate: SplashScreenVMProtocol! returns nil. Is it better way to start app from begining when app is awake again?