1

I am using Core Data in my iPhone apps. It works fine when I use it in the simulator but when I deploy it to device, the value of my Core Data store is not found to my application. If I insert a value on the iPhone it finds the value in Core Data.

I need to keep the value of Core Data while deploying it to iPhone.

Please help me if anyone knows how to do this.

3
  • You're more likely to get a helpful answer if you give us something to go on. Things like "I am trying to do X, my code looks like Y, on the device I expected it to do A but instead it did B. My console output said 'C'." Commented Feb 25, 2011 at 15:13
  • Also: msmvps.com/blogs/jon_skeet/archive/2010/08/29/… Commented Feb 25, 2011 at 15:18
  • The OP author is clearly not a native-Enlish speaker. I believe he is asking why a preexisting persistent store works fine in Xcode but fails on device. Commented Feb 25, 2011 at 17:29

2 Answers 2

2

All files including with the app install on device must be in the unalterable app bundle which makes the files readonly.

If you include a Core Data persistent store file with the app, it resides inside the app bundle and can only be added to the persistent store coordinator as as a readonly store. Any attempt to write to it will produce an error.

You usually do need a readwrite store so the solution is to use standard file operations to copy the included persistent store file from the app bundle and then into the app's Documents or Library folder. When the app launches, the app delegate checks for the presence of the store before initializing the Core Data stack and copies it over if it hasn't already done so.

You also have the more advanced option of using two stores for one stack, one readonly and another readwrite. However, that method introduce complexity into the data model and is usually not needed.

Sign up to request clarification or add additional context in comments.

Comments

1

It's quite easy ...

  • prepare your CoreData database file
  • name it, InitialData.sqlite for example
  • add it to your application bundle

... and add something like to this just before NSPersistentStoreCreation allocation & initialization.

NSString *storePath = ... your store path for persistent store coordinator ...;

NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
  NSString *initialStorePath = [[NSBundle mainBundle] pathForResource:@"InitialData" ofType:@"sqlite"];
  if (defaultStorePath) {
    [fileManager copyItemAtPath:initialStorePath toPath:storePath error:NULL];
  }
}

I assume you want to copy some default data to your application. That's all I can deduce from your question - it's quite unclear what you really want.

1 Comment

defaultStorePath isnt declared, did you mean initialStorePath?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.