I meet a problem when I import data in an ios app.
The data is stored with coredata. I was trying to import the data with a button. After it is clicked, the data, which is firstly stored in a txt file in JSON format will be stored in the sqllite file.
My question is this, it is very slow to import such amount of data and it is not friendly for user to click button or wait to import the initial data. Is there a better way to import data?
Thanks.
It depends. For example you could just import data in
– applicationDidFinishLaunching:
or when the user touches a specific button.In both cases I would import data in background. This allows you to avoid UI freeze (if you have a lot amount of data) and to display a sort of progress indicator. Maybe the user could be more happy to know what is going on.
To import data in background, you could just use new iOS 5 API for Core Data or follow Marcus Zarra tutorial on importing-and-displaying-large-data-sets-in-core-data/.
Another way could be to start with a pre-populated db. Create a dummy project where you populate that db (with your JSON file) and then use that db in your real application project.
Hope that helps.
Edit
Why not?
I'm not sure I got the point here. Here what I mean with preload and import existing data. You need to ship the db file with your app when you submit it to the app store. For example within the application directory. You could ship it also within the bundle. But in this case pay attention since the db file it is read-only (you need to move somewhere elese if you want to modify).
I suggested you to create a dummy project since it's my personal way to do thing when I need to create a prepolutade db. This allows you to maintain cleaner your project. But you can also populated that db in your real project. If you follow the first way you can simply move the sql file in the application directory for your app and say to core data to read that.