Tips for storing data?

Hello all - I’ve completed the first paid-for ‘Code With Chris’ course bundle (‘iOS Foundations’ which is fantastic by the way, I highly recommend it!) and I’m now trying to build an experimental app to put my new knowledge to the test.

I wanted to ask anyone’s advice on the best option for storing data on a user’s iPhone (for my experimental app) for long-term use.
To explain: my app needs to store an array, but one that is continually being added to over time.
The items in the array are small in size (basically just a class that consists of mostly just ‘Double’ values and some small ‘Strings’ items), but the number of items would grow over time (say 50,000+ items over a few years), so this piece of data could get to, say, 5MB+ in size.

I’d like to store this data, but for continuous and long-term reference and use (by the app).
I don’t want to use cache obviously (as it’s important data), and i don’t want to upload to a cloud-based database, nor do I really want to store directly in the user’s iCloud folder (since it’s application data, which the user doesn’t need file access to directly).

What would be the best method to store data like this on the device? (I’ve heard that a ‘Library’ folder exists with the app’s ‘Data Container’, which is backed up by a user’s normal iPhone backups… which sounds perfect for this!.. but i’ve no idea about how to store data to this.)

Also, in terms of storage formats, I know from the ‘Code With Chris’ course, that I could store it as JSON, but i’m wondering whether there’s a better, more ‘raw’, form to store data in (as it would save encoding/decoding each time, as the data is being continuously accessed and added to).

Any hints/thoughts would be welcome…

Many thanks in advance.

A Quinn

i’ll still say that just save it to a cloud database, maybe firebase, a free plan should be more than enough for this.

Because along with the database it comes with a cloud storage (firestore) for photos and videos if you need them. it also has built-in persistence( which simply means data can still be accessed even when offline as long as you already loaded it )

storing to a device like NSUserdefaults should be fine, not sure about the capacity to store data that size though, problem is that its not really “secure” and can’t be opened if lets say the phone gets accidentally damaged, you can’t retrieve old data just “storing” it in a cache

On device options, there’s CoreData or SQLite too!

CoreData is a framework by Apple

SQLite is a third party database solution.

I’m confused why it has to be an array?? I’d suggest using SQLite because it’s an actual database, and making sure you make the proper schema for this data

Many thanks to both of you @fuerte.francis and @MikaelaCaron !

@fuerte.francis - thank you, but i’m specifically wanting to ensure all data is stored on the phone in this instance (not solely due to connectivity, but for other reasons). NSUserDefaults I think might perhaps work, but I got the impression online that these were more for user ‘preferences’ as opposed to main data sources, and that there might be a 1MB file size with each of these NS UserDefault keys (but will look more into them to check though; thank you!)

@MikaelaCaron - Many thanks also to you (that’s twice you’ve helped me out now! thank you!)… I shall do some research into ‘CoreData’ and ‘SQLite’ in that case… my guess (as an amateur) is that SQLite might require off-device storage (?) in which case CoreData sounds ideal. And, apologies, when I said that i need to store an array… it was just me being an amateur lol… My app creates an array, that gets continually added to, and I just needed somewhere to store that long term… but from your response, perhaps when data gets stored it doesn’t get stored as an ‘array’ but rather in some other format… but will look into those things ‘CoreData’ and ‘SQLite’ they are both useful pointers… thank you

1 Like

For the benefit of others that may come across this thread with a similar issue in the future, after MikaelaCaron’s helpful advice, I did some googling and thought i’d share a simple article I came across that gave a brief but helpful comparison between CoreData and SQLite: https://cocoacasts.com/what-is-the-difference-between-core-data-and-sqlite/

1 Like

if thats the case then i highly recommend to just use sqlite, its good and can store a lot of files and data, it is also familiar to use because it works just like MySQL but compact and already inside your phone/devices

check out this article https://www.appcoda.com/sqlite-database-ios-app-tutorial/

Ah! that link’s fantastic! thanks ever so much @fuerte.francis … really appreciate your help, thank you.

1 Like