Best practice for storing temporary data in swift

Viewed 12722

I have developed an app that gets a lot of JSON data from server and shows on different VCs.What I did was getting data and setting data to static variables, so I can access data in all my app.

Now I noticed that I could have used CoreData or maybe UserDefaults instead of Static variables.

My question is "What is the best practice for this type of apps and why exactly?"

NOTE: The app doesn't need to work offline and save data for showing in offline mode.

5 Answers

You can create data model class to store temporary data from JSON response.

Example of Data Model class : -

class RootClass : NSObject, NSCoding{

    var array : [Int]!
    var booleanField : Bool!
    var custArray : [CustArray]!
    var nullField : AnyObject!
    var number : Int!
    var object : Object!
    var string : String!


    /**
     * Instantiate the instance using the passed dictionary values to set the properties values
     */
    init(fromDictionary dictionary: [String:Any]){
        booleanField = dictionary["Boolean"] as? Bool
        nullField = dictionary["Null"] as? AnyObject
        number = dictionary["Number"] as? Int
        string = dictionary["String"] as? String
        if let objectData = dictionary["Object"] as? [String:Any]{
            object = Object(fromDictionary: objectData)
        }
        custArray = [CustArray]()
        if let custArrayArray = dictionary["CustArray"] as? [[String:Any]]{
            for dic in custArrayArray{
                let value = CustArray(fromDictionary: dic)
                custArray.append(value)
            }
        }
    }

    /**
     * Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
     */
    func toDictionary() -> [String:Any]
    {
        var dictionary = [String:Any]()
        if booleanField != nil{
            dictionary["Boolean"] = booleanField
        }
        if nullField != nil{
            dictionary["Null"] = nullField
        }
        if number != nil{
            dictionary["Number"] = number
        }
        if string != nil{
            dictionary["String"] = string
        }
        if object != nil{
            dictionary["object"] = object.toDictionary()
        }
        if custArray != nil{
            var dictionaryElements = [[String:Any]]()
            for custArrayElement in custArray {
                dictionaryElements.append(custArrayElement.toDictionary())
            }
            dictionary["custArray"] = dictionaryElements
        }
        return dictionary
    }

    /**
     * NSCoding required initializer.
     * Fills the data from the passed decoder
     */
    @objc required init(coder aDecoder: NSCoder)
    {
        array = aDecoder.decodeObject(forKey: "Array") as? [Int]
        booleanField = aDecoder.decodeObject(forKey: "Boolean") as? Bool
        custArray = aDecoder.decodeObject(forKey: "CustArray") as? [CustArray]
        nullField = aDecoder.decodeObject(forKey: "Null") as? AnyObject
        number = aDecoder.decodeObject(forKey: "Number") as? Int
        object = aDecoder.decodeObject(forKey: "Object") as? Object
        string = aDecoder.decodeObject(forKey: "String") as? String
    }

    /**
     * NSCoding required method.
     * Encodes mode properties into the decoder
     */
    @objc func encode(with aCoder: NSCoder)
    {
        if array != nil{
            aCoder.encode(array, forKey: "Array")
        }
        if booleanField != nil{
            aCoder.encode(booleanField, forKey: "Boolean")
        }
        if custArray != nil{
            aCoder.encode(custArray, forKey: "CustArray")
        }
        if nullField != nil{
            aCoder.encode(nullField, forKey: "Null")
        }
        if number != nil{
            aCoder.encode(number, forKey: "Number")
        }
        if object != nil{
            aCoder.encode(object, forKey: "Object")
        }
        if string != nil{
            aCoder.encode(string, forKey: "String")
        }
    }
}

Simple way to create data model class : - jsoncafe.com

Related