Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for storing temporary data in swift

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.

like image 538
Saeed Hajizade Avatar asked Aug 14 '17 06:08

Saeed Hajizade


People also ask

When should I use Core Data vs UserDefaults?

The next time you need to store data, you should have a better idea of your options. Core Data is unnecessary for random pieces of unrelated data, but it's a perfect fit for a large, relational data set. The defaults system is ideal for small, random pieces of unrelated data, such as settings or the user's preferences.

How is data stored in Swift?

By default, all the variables and constants that you allocate in Swift are stored in memory, so they get lost when users quit the app. Different types of data can be saved to the device locally, using different tools (Swift APIs).


2 Answers

You should not store data to UserDefaults, user defaults is just a key value based file which is mostly used to store some data about user, for example :

doNotShowAdds : true
isUserLogedIn :true.

You should not use keychain either since keychain is encrypted container where u store critic data about user, for example : password.

You don't need to use database

My opinion is that you should not use global variables, mainly I try to make viewController data specific for itself, the data should not be accessed from anywhere in your project. When to use global variables in Swift

Mostly I make service protocols with implementations for specific view controllers. Anyways, each controller should have its own part of data for example

class MyViewControllerOne {
    private var data :Array<DataExampleOne>;
}

class MyViewControllerTwo {
    private var data :Array<DataExampleTwo>;
}

Once you load data from your API, the data will be there, until you close your app.

You can always make another class which will contain this data and maybe predicates for cleaner approach for example :

protocol MyViewControllerOneService {
    func search() -> Array<DataExampleOne>;
}

class MyViewControllerOneServiceImpl :MyViewControllerService {
public var data :Array<DataExampleOne>;

public func search(searchString :String) -> Array<DataExampleOne>{
    let predicate = NSPredicate() ...
    let filteredArray = self.data.filter { evaluate...}
    return filteredArray;
    }
}

I use similar approach, instead of saving data in my service classes (business logic), I got repositories which I use to get data from database. Since you don't have any database this approach is okay.

and then instead of

class MyViewControllerOne {
    private var data :Array<DataExampleOne>;
}

you can use

class MyViewController {
    private var myViewControllerService :MyViewControllerOneServiceImpl;
}

Hope it helps, Best regards!

like image 76
LightinDarknessJava Avatar answered Sep 20 '22 14:09

LightinDarknessJava


Discussion:

"What is the best practice for this type of apps and why exactly?" is a very good question. It would help if you described what type of app you are developing.

Since you only seem to need/want the data while the app is running then I guess the best way is to keep it in memory on some singleton or static variables which you are already doing.

But if you don't want to store your data after the app is relaunched then you might want your data to expire after some duration since app may be opened for a very long time.

Then the question is how you want to receive your data in runtime. Assume you have cached data and you wish to refresh the data anyway. Then you may design your data management so your closure to fetch items may be call multiple times. Assume having something like this:

    func fetchUsers(callback: ((_ users: [Any]?, _ isFromCache: Bool) -> Void)) {
        if let cachedUsers = MyCache.fetch(.users) {
            callback(cachedUsers, true)
        }

        APIManager.fetch(.users, { users, error in
            if let users = users {
                MyCache.insert(.users, items: users)
            }
            callback(users, false)
        })
    }

Then let's say you want to show these users in some table view you would do something like:

    func refreshData() {
        self.startActivityIndicator() // Start some minor activity indicator
        fetchUsers { (users, isFromCache) in
            self.users = users // Assign users whatever they are
            if users == nil {
                // Show error here
            }

            if  isFromCache == false {
                self.stopActivityIndicator() // Only stop activity when fresh data were received so user know he may expect the UI to change
            }

            tableView.reloadData()
        }
    }

This will design your user interface part of the data fetching but has nothing to do with where and how your data is stored. For instance MyCache should still chose weather the cached response is still valid or outdated. And it is MyCache which decides to either store data into some database or only in-memory.

Some storing procedures:

To keep the design in previous snippets you may have a static class MyCache which has an string enumeration for stored types. It then has a static dictionary [String: Any] which contains cached data so a fetch method looks something like

func fetch(type: FetchType) {
    return cacheDictionary[type]
}

The insertion is pretty similar then.

To add date (or any other data in there) you may define it as [String: [String: Any]] and use static keys so then your data would be cacheDictionary[type]["date"] as? Date and data as cacheDictionary[type]["data"].

You may then expand your cache to for instance use user defaults like:

func fetch(type: FetchType) {
    if let inMemory = cacheDictionary[type] {
        return inMemory
    } else if let inDefaults = UserDefaults.standard.object(forKey: type) {
        cacheDictionary[type] = inDefaults // Keep it in memory for net time
        return inDefaults
    } else {
        return nil
    }    
}

This is for storing direct JSON data or objects. Same can be applied for storing data (serializing it with JSONSerializer) into files. You could create a base path in temporary directory or in library then use file names by type. Temporary directory may be cleared by the system which you might want and library directory will be persistent.

Then there is database:

The database is a whole other story. It makes no sense to apply a database into the same procedure as above because it is an overkill and you use no benefits of the database at all.

For database you would create models for each of your responses and insert the data into database then notify your UI controllers to update. That means callbacks are not the best practice but rather delegates. You would have for instance DatabaseManager and on view did appear you would call DatabaseManager.shared.delegate = self then Users.fetchNewUsers() and waited for the delegate to trigger. Then on delegate you would fetch objects from database or create a fetch result controller...

There probably very many other ways but I hope this will give you some idea about how to store data when to use which.

like image 42
Matic Oblak Avatar answered Sep 16 '22 14:09

Matic Oblak