mardi 7 juin 2016

Init method for a singleton

I have my file myAPI.swift, and two objet Round and GameStats. My Round object also have an attribute GameStats. So what I want to do is to get the GameStats property that I store inside my users defaults then assign it inside my Round object.

class myAPI: NSObject {
    static let sharedInstance = myAPI()

    var currentStats: GameStats?
    var currentRound: Round?

    private init(){
       super.init()
       self.loadData()
       NSLog("Stats have been reload: \(self.currentRound?.gameStats)") // Return nil
       // If I try to add this line the app stop running and nothing happens
       NSLog("Test Singleton: \(myApp.sharedInstance.currentRound?.gameStats)")
    } 

     func loadData(){
            let backupNSData = NSUserDefaults.standardUserDefaults().objectForKey("backupNSData")
            if let backupNSData = backupNSData as? NSData{
                let backupData = NSKeyedUnarchiver.unarchiveObjectWithData(backupNSData)
                if let backupData = backupData as? [String:AnyObject] {
                    guard let round = backupData["currentRound"] as? Round else {
                        print("error round loaddata")
                        return
                    }
                    self.currentRound = round
                    guard let stats = backupData["stats"]  as? GameStats else {
                        print("error guard stats")
                        return
                    }
                    self.currentRound.gameStats = stats
                   NSLog("Stats reloaded: \(stats)") // This is not nil it works here
                 }
          }
    }

When my app crash I call this function to save the data

func backupData(){
        var backupData:[String:AnyObject] = [String:AnyObject]()
        if let round = self.currentRound {
            backupData["currentRound"] = round
            ColorLog.purple("Stats saved inside Round \(round.gameStats)")
        }
        if let stats = self.currentStat {
            backupData["stats"] = stats
            ColorLog.purple("Stats saved : \(stats)")
        }
        let backupNSData = NSKeyedArchiver.archivedDataWithRootObject(backupData)
        NSUserDefaults.standardUserDefaults().setObject(backupNSData, forKey: "backupNSData")
        NSUserDefaults.standardUserDefaults().synchronize()

    }

So I have two question,

1) Is it normal that I can't call my singleton like myApp.sharedInstance.currentRound.id = 5 (for instance) inside the init() (I guess it is but I didn't find anything about that)

2) Why in my init my first NSLog self.currentRound?.gameStatsis nil when in the functionloadData()` it wasn't ? It seems like it's loosing its reference since we are leaving the function.

What am I doing right now is add a currentStats property in my singleton then when I retrieve data instead of doing self.currentRound.gameStats = stats I do self.currentStats = stats then self.currentRoud.gameStats = self.currentStats and If I do that then it's working, don't really know If I am doing the things right here.

Also my two objects Round and GameStats conforms to NSCoding protocol as I implement for both of them @objc func encodeWithCoder and @objc required init?(coder aDecoder: NSCoder) method.

Thank for you help.

Aucun commentaire:

Enregistrer un commentaire