Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UserDefaults or Core Data? [duplicate]

I need to store some objects in my app (Xcode 9.1, Swift 4). Currently, I'm storing it like this:

class myItemManager {

    var items: [myItem]? {
        didSet {
            Cache.c.items = items
        }
    }

    static var manager = myItemManager() {
        didSet {
            myItemManager.manager.items = Cache.c.items
        }
    }
}

class Cache {

    private let uds = UserDefaults.standard

    static let c = Cache()

    var items: [(myItem)]? {
        get {
            if let decoded = uds.object(forKey: Constants.Cache.myItem) as? Data {
                let decodedItems = NSKeyedUnarchiver.unarchiveObject(with: decoded) as? [myItem]
                return decodedItems
            } else {
               return nil
            }
        }
        set {
            let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: newValue as AnyObject)
            uds.set(encodedData, forKey: Constants.Cache.myItem)
            uds.synchronize()
        }
    }
}

First, I'm downloading items (from JSON). After I download it, I just save it, like this:

myItemManager.manager.items = downloadedItems

Of course, "myItem" subclasses NSObject and NSCoding (allows to save in UserDefaults).

Is this a good approach? Or should I use Core Data? In the future, I will have to filter this data and present it in UICollectionView.

like image 811
Bartosz Woźniak Avatar asked Nov 21 '17 11:11

Bartosz Woźniak


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.

What is UserDefaults good for what is UserDefaults not good for?

UserDefaults caches the information to avoid having to open the user's defaults database each time you need a default value.

How much data can you store in UserDefaults?

There is no specific number attached to “a small amount”, but everything you store in UserDefaults will automatically be loaded when your app launches – if you store a lot in there your app launch will slow down. To give you at least an idea, you should aim to store no more than 512KB in there.

When should I use Core Data?

Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.


2 Answers

NSUserDefaults as the name suggests (vaguely) should be used for storing preferences and app settings only. You should not be storing critical data and or user data into them.

CoreData is a full fledged persistent framework which supports large data transactions. CoreData allows you to build relational entity–attribute model for storing user data.

Please note that CoreData is a framework and can use SQLite, binary formats to store data (the default templates always use SQLite).

Example:

App preferences like show notifications, toggle switch settings, UISegmentedControl settings all go into NSUserDefaults. Whereas any data you might fetch using a web service like a list of all cities in a country, list of todos for a user go into CoreData.

PS: I don't recommend wiki for technical write-ups but to get quick understanding on CoreData, please refer here!

Pros of NSUserDefaults:

  • Easy to store and retrieve data.
  • Useful for storing default values with minimum fuzz.

Cons of NSUserDefaults:

  • Not suitable for large data sets

  • Performance hit when you try to store and load large amount of data All or nothing approach

Pros of CoreData:

  • Reliable framework to interact and query against data

  • Can be extremely fast when setup correctly (with relationships)

  • Powerful capabilities

Cons of CoreData

  • Takes time to master and learn the core concept

  • Needs proper app architecture design to be efficient

  • You cannot have a learn and implement in a day approach with CoreData

  • As you improve your app, you need to improve your data architecture as well

  • Migrating to new versions can be a pain if you are not careful.

Reference From HERE.

like image 108
Dharmesh Kheni Avatar answered Sep 18 '22 21:09

Dharmesh Kheni


If the data has nothing to do with user preferences, then don't store it there. What about serialising the data to an external file in your container before going the full monty with Core Data?

like image 44
mschmidt Avatar answered Sep 17 '22 21:09

mschmidt