Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSUserDefault vs Global Variable vs CD vs Singleton - a specific example

Tags:

ios

In my fitness app I store various values that needs to be access throughout the app in varying degrees. goals, max, min, class names, fontColors, strokes, gradientColors etc. Since the app is being created while I am learning to code, I use a mix of all of the above storage methods - a right mess. To the point, the goal variables (like 10,000 steps or a target weight) are presented throughout the app and used in the code often, but the user do not change them very often. It is paramount that the goals persist, also throughout version updates, as the user would be annoyed if he/she would have to add them over again. What is the best practise for storing the goal variables, there might be 20 of them or so?

like image 941
KML Avatar asked Feb 06 '26 19:02

KML


2 Answers

The common rule is following: information that could be easily recreated should be stored in memory whereas user typed information/progress should persist somewhere.

Core data vs NSUserDefaults. Store basic information (like Int, String, etc) in NSUserDefaults (it is the easiest way) and complex information (arrays, complex objects, object relations) in Core Data (take a look at Realm – it is better alternative to Core Data).

Global variable vs Singleton. Global variables are in most cases are a very poor practice. They should belong to specific class and you can create singleton in it if needed (example – class AppSettings that has singleton in it and manages basic settings of the app).

Considering you have 20 goals which can be changed by a user (so can not be recreated) I recommend you using CoreData or Realm.

Also take a look at HealthKit – it might be a good idea to store user's progress (e.g. burned calories) there.

like image 73
Nikita Arkhipov Avatar answered Feb 09 '26 01:02

Nikita Arkhipov


The first thing to ask yourself is: what does my application do? It sounds like a fitness app, so fitness data should be stored in your data model, which is the repository for any data that fulfills the business logic requirements of your app (i.e. keeping track of my users' fitness data).

There are several persistence mechanisms which people use for their data models, the easiest of which is storing your data in a dictionary, and reading/writing that to the file system -- this would be acceptable for a prototype, but as you add features and your data model becomes more complicated, you'd need to consider NSCoding or CoreData.

But if you store fitness data, you might want to consider Healthkit, which allows you to store fitness and health data directly, allowing you to share different kinds of health data with other apps.

NSUserDefaults is typically a place to store user preferences or any data which are outside of your business logic. Unless your app is very simple, you will want to keep your data model out of NSUserDefaults, for various reasons.

Here's how you'd do it using a dictionary:

class MyDataModel {

    var goalSteps : Int {
        get { return dict["goal1"] as? Int ?? 0 }
        set { dict["goal1"] = newValue }
    }

    var goal2 : String {
        get { return dict["goal2"] as? String ?? "" }
        set { dict["goal2"] = newValue }
    }
    // ... Etc etc ...

    // Load the data model from a path
    init(path: String)
    {
        dict = NSMutableDictionary(contentsOfFile: path)!
    }

    func Save(toPath : String) {
        dict.writeToFile(toPath, atomically: true)
    }

    private var dict = NSMutableDictionary()
}
like image 22
joeybladb Avatar answered Feb 08 '26 23:02

joeybladb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!