I´m having some fun with swift and I´m trying to make a simple game. I got a few variables that changes during the game. What´s the best practice for saving those variables if <
When I started with iOS programming, I first stored data in custom text files. Then I saved arrays to disk, used NSUserDefaults
, and other tools. At the end, I decided to move to CoreData since I had to save more and more data and my application was becoming too slow.
Looking back, it would have been much better if I had used CoreData from the beginning. It is very powerful and migrating to CoreData later takes a lot of time.
If you would like to use CoreData, I recommend to start with a good tutorial or a good book. I liked the book "Learning CoreData for iOS" by Tim Roadley.
An interesting alternative may be Realm. I haven't used it yet but it may be worth a look.
However, it really depends on your application. If you do not need to save a lot of data, simpler approaches may be sufficient (NSUserDefaults, etc.).
A word of caution: Apple recommends to save data continuously and not wait until the app enters into the background state. At that time, the app may be suspended at any time and you cannot be sure that your data will be saved in time.