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

It is not user friendly to import the data when the app begins.

Why not?

So I was trying to put the data - the db file into the archive and send it to app store. In this way, I was wondering if I could get the db file during test, which is finished importing the data and the initial data is acceptable. And put this test db file in the archive and publish on appstore. So user do not need to import the data at first. Just use the copy of the testing data

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top