views:

279

answers:

2

For an iPhone app that has to have a bunch of data inserted before the user can do their thing on first launch, where is the correct place (in the code) to insert that data? I'm looking at between 700 - 800 records total between a few tables.

I initially tried doing it in applicationDidFinishLaunching:. This worked fine for the iPhone 3gs, but caused a consistent first-launch crash on the 3g. After digging into the problem, i found that on the 3g, the app wasn't responding fast enough (presumably because it was busy dumping loads of data into the persistent store), so the OS was killing it under the assumption it was unresponsive.

To fix this problem, I moved the data initialization process out of appdidfinishlaunching and spawned another thread from the first view controller that shows upon launch. This works most of the time, but every once in a while the app crashes with a 134030 error in core data, which according to the apple constants reference, is:

NSPersistentStoreSaveError

Error code to denote that a persistent store returned an error for a save operation. This code pertains to errors such as permissions problems.

This error pops up when calling save: on the managedObjectContext.

+3  A: 

Ideally towards the end of development you would populate this data into a Core Data store and add that to your project. On first run, you then copy that store from the app bundle over as the user's Core Data store. This should work for your case.

In the case where this is not possible, one would perform the import on a background thread.

gerry3
Excellent solution, and it makes perfect sense. A wise man once told me that I suffered from "paralysis by analysis", meaning that I often overlook the simplest solution by over-analyzing the problem :)Thanks a bunch!
jtrim
A: 

I am new to this iphone development and Core Data. How exactly do you populate the data into a Core Data Store? My application needs to have a data store but the user will not be able to modify the data once it has been initialized. Anyway... any additional information would be great.

David

David
Welcome to StackOverflow! If you have another question, please click 'Ask a Question' at the top of the page, and post your question there. That will ensure that as many people as possible see it, and by creating a new question, can help other people with the same problem find the information.
Chacha102
To expand on Chacha102's comment, this is a community website. When you ask an individual person a question in comments, you're much less likely to get an answer than if you post a new question for the community to see / answer. And for those reputation sticklers out there, if I were to answer your question here as a comment, I wouldn't gain any reputation for a correct answer.
jtrim