1

I am currently making an app where I need to store large amounts of data persistently, much of which is contained within custom classes.

I am used to using UserDefault for storing data, but I have read that UserDefault doesn't work well with large amounts of data. What are my options, and is it possible to store it all by using UserDefault?

The game is a football manager game, and I have 30 teams, and every team has around 25 players, and every player has a long list of variables like games, goals, club etc that needs to be stored after each game

Tarjerw
  • 505
  • 1
  • 5
  • 12
  • user defaults are not bad but you can go with a file (or multiple files) or a database. It really depends on your needs. – Sulthan Mar 01 '18 at 18:28
  • 1
    No, do not store data in UserDefaults. – rmaddy Mar 01 '18 at 18:43
  • I would suggest to start with NSCoding / NSKeyed​Archiver http://nshipster.com/nscoding/ – nspavlo Mar 01 '18 at 18:44
  • 2
    You will get much better answers if you provide more details about your data. – rmaddy Mar 01 '18 at 18:44
  • 1
    @nspavlo Why? With Swift 4 you should be using the new Codable features, not the old Objective-C NSCoding. – rmaddy Mar 01 '18 at 18:45
  • It's basically the same thing. I linked this article for comparison of Core Data to NSKeyedArchiver. Just create nice interface for your persistent store, later when it's necessary implementation can be changed without any issues. – nspavlo Mar 01 '18 at 18:49
  • @nspavlo No, the new Swift Codable is written in Swift. The old `NSCoding` requires that your Swift code take on a lot of unnecessary Objective-C baggage. And neither have anything to do with Core Data. – rmaddy Mar 01 '18 at 18:50
  • Codable will not store anything to disk by itself. – nspavlo Mar 01 '18 at 18:58
  • 3
    See for example: https://stackoverflow.com/q/16407594/1187415, https://stackoverflow.com/q/32951538/1187415, https://stackoverflow.com/q/24970074/1187415, https://stackoverflow.com/q/35762561/1187415 – Martin R Mar 01 '18 at 19:25
  • @nspavlo Of course not. Just as `NSCoding` doesn't by itself. You use something like `PropertyListEncoder/Decoder` and then write/read the `Data`. – rmaddy Mar 01 '18 at 20:03
  • You can use Codable protocol and save the json data locally or if needed upload it to a server. https://stackoverflow.com/a/47579322/2303865 – Leo Dabus Mar 02 '18 at 01:37

2 Answers2

2

Please have a look at Core Data, an Apple framework which helps you do exactly this: store large amounts of data persistently. Apple has loads of documentation. You might want to start here:

Core Data Programming Guide

Tom E
  • 1,530
  • 9
  • 17
  • Core Data is also very complex. Sometimes using just files is better. It depends on the use case. – Sulthan Mar 01 '18 at 18:42
2

I would recommend using Realm. It abstracts the complexity of working with SQLite and it is easier than Core Data.

https://realm.io/docs/swift/latest/

Tiago Couto
  • 351
  • 3
  • 9