Lukas Junokas
Lukas Junokas

Reputation: 91

Advise where to store data for iOS app

I have created an app using ionic and cordova and now I want to remake it on iOS. I am working with iOS for the first time, and I cannot figure out how to store data. For example: I have a form where user has to input some data, but the inputs are not in one view, there must be several views. I used to create empty array and just put everything step by step, but now i can't use same view controller on multiple views. Tried to do it with core data, but core data cannot store arrays. My object would look something like this:

var sampleArray = (
duration: 13,
dayOfTheWeek: Thursday,
personList: [
  (name: Rocky,
   age: 26),
  (name: Ralph,
   age:23)
  ]
)

The question would be: How could I make an input form which would be on several views and where should I store the data, and later I would be able to store all the data into core data?

Upvotes: 3

Views: 1402

Answers (2)

Tomasz Bąk
Tomasz Bąk

Reputation: 6204

You can work with persistent data in several ways on iOS.

User Default

This is a tool that is used to store small amounts of information like user settings, preferences etc. Don't use it for data that will scale with application usage (e.g. like notes in notepad app). Documentation will answer all your questions about User Defaults.

Database

You have Core Data as an out of the box solution which is build on top of the SQLite and takes some time to learn, but from my experience it's worth the effort. You are free to use pure SQLite or other database type, but it requires more code and probably custom frameworks.

Text files

You can use arbitrary XML, JSON or CSV files to store your data. Tooling is rich (e.g. NSXMLParser or SwifyJSON just to name two) and if you look on Github, you will find what you need. You can also use build in combination of NSCoder and NSKeyArchiver / NSKeyUnarchiver which are easy to grasp.

Binary files

Finally, for a local storage you can use binary files i.e. images. This is too advanced topic to cover here, but I want to share an example of Open Raster file format. It is used to save informations for drawing apps (eq. GIMP) and inside, it is basically an XML file and a bunch of images compressed to zip and named as .ora file. Creating your own specification for a hybrid format is not that hard.

Network repository

Just to not overlook other methods, you can use remote database API to store data outside of the device, but of course you need your own host and some backend skills.

I hope I didn't miss something important. I just wanted to sum up this knowledge in one place for future reference.

Upvotes: 3

Jan ATAC
Jan ATAC

Reputation: 1262

As the first comment says, your question is quite large. When you say 'one form on several view', I consider it as 'one form per view'. Keep It Simple S... ;) (Except if you use page control for your form.)

Basically, you have three ways to store data :

  • NSUserDefaults : Store data in Dictionary for later use
  • File : Save data to a File (why not .csv like ?)
  • CoreData : You can persist arrays as binary data in Core Data

There are numerous tutorials on these topics. www.raywenderlich.com site is a good one to begin...

Upvotes: 2

Related Questions