How to store an updated set of questions and answers in iOS/Swift and persist it

417 views Asked by At

I'm starting a relatively simple app which presents a series of multiple choice questions. There will be 4 possible answers (A/B/C/D) and more than one choice can be correct. While manipulating such an object (one instance of a question with its accompanying answers) I've created the following data structure:

public class QAData {
var questionText: String
var answers: [String] // Will contain 4 separate answer options
var correct: [Bool] // contains 4 boolean values to match against options of answers to record correct answer/s
var selected: [Bool] // Will be updated to true if user selects relevant answer option

}

My question is what is the best way to store all the questions and answers in my app which also allows me to update the 'selected' attribute and store it across app launches? I'll initially have 100 questions however this could increase over time up to 1000.

Options I've considered is having an array containing all the data which is saved to user defaults - perhaps not really what it's designed for?

Persisting the object using NSCoding?

Or having the data in a SQLite database?

Any recommendations?

3

There are 3 answers

0
Twinkle On

I guess saving of large amount of data in UserDefault is not a good way. It will badly affect to your application performance later on as your number of question might be increase.

I think coredata framework suit to your requirement . You can manage one entity in coredata which will persist all your questions with its four options, its boolean answers and user's selected answers. And you can easily save,update and retrieve data from local database using coredata.

3
Kishan G On

You can save data in .Plist File or CoreData/Sqlite database. If all Q/A are static then prefer .plist file is better way.

0
Jeyhey On

If the Q&A are more less static and get only updated by you, store them in an asset (e.g in an HTML file) and read this file to access them.