Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

ios - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...