0

I'm trying to make a little dictionary on my app by using Core Data.When you use a Master-Detail application, xxx.sqlite file gets created in the user's Documents folder. Now before I start the application for the first time, I changed the code as below because I wanted the xxx.sqlite file in the app:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
 if (_persistentStoreCoordinator != nil) {
 return _persistentStoreCoordinator;
}
 //NSURL *storeURL = [[self applicationDocumentsDirectory]
 URLByAppendingPathComponent:@"CoreDataFileTest.sqlite"];
 NSString* path= [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"CoreDataTest.sqlite"];
NSURL* storeURL = [[NSURL alloc] initFileURLWithPath:path];

I thought the last two line would make a xxx.sqlite file in my app project folder. Actually, it didn't. But the app works fine. This means that the xxx.sqlite file is embedded in the app itself? Thank you for your time.

rmaddy
320k44 gold badges548 silver badges591 bronze badges
asked Jul 27, 2013 at 4:57
4
  • Remove app from simulator, and run app again. Commented Jul 27, 2013 at 4:59
  • @Bhargavi Thanks for your advice. I removed my app from simulator and ran it, but I still can't find xxx.sqlite file in my app folder and the app still works! One thing I notices was that in the ' Cache' folder, I found these 3 files: Cache.db, Cache.db-shm and Cache.db-wal. Do they have something to do with this strange behavior? Commented Jul 27, 2013 at 9:14
  • When I run find . -name "MyModel.sqlite" -print command on the terminal, I find 'xxx-xxx-xxx.../<my app name>/xxx.sqlite. I don't get it??? Commented Jul 27, 2013 at 9:38
  • @Bhargavi Hi. After the comment above, I tested it on my iPhone, not on Simulator. It failed; this means that simulator caches the data and reads from it. Then I have decided to make a sqlite file in Documents first, later I'll move it to the app folder. Thanks again. Commented Jul 29, 2013 at 0:40

2 Answers 2

1

If, as I understand your question, you would like to include a default database with your app (i.e. the .sqlite file will be included as an asset of your project, then you need to copy it from the app bundle into your documents directory at startup).

Then call this function from your didFinishLaunchingWithOptions method in your app delegate.

- (void) copyDefaultDB
{
 // If we are running the app for the first time, then copy our default database across from our app directory
 // into our user directory 
 NSString *filePath;
 // Get the path to the documents directory and append the databaseName
 NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDir = [documentPaths objectAtIndex: 0];
 NSFileManager *fileManager = [NSFileManager defaultManager];
 filePath = [documentsDir stringByAppendingPathComponent: @"CoreDataFileTest.sqlite"];
 // If the database already exists then return without doing anything
 if (![fileManager fileExistsAtPath: filePath])
 {
 // Now copy the new shiny one in
 NSString *filePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"CoreDataFileTest.sqlite"];
 // Copy the database from the package to the users filesystem
 [fileManager copyItemAtPath: filePathFromApp 
 toPath: filePath 
 error: nil];
 }
}
answered Jul 27, 2013 at 13:52
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for a good piece of code. I understand this is how to copy the sqlite file in Documents into my project folder. But I still don't understand why my app still works after deleting the sqlite file in Documents folder and by switching the path to the project directory. Is my sqlite embedded in the app? Is that why I can't see it in the project folder?
Hi. I'll do this after sorting out my app. Thanks again.
0

Try with this code. Write this inside AppDelegate.m

- (NSManagedObjectContext *) managedObjectContext {
 if (managedObjectContext != nil) {
 return managedObjectContext;
 }
 NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
 if (coordinator != nil) {
 managedObjectContext = [[NSManagedObjectContext alloc] init];
 [managedObjectContext setPersistentStoreCoordinator: coordinator];
 }
 return managedObjectContext;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
 if (persistentStoreCoordinator != nil) {
 return persistentStoreCoordinator;
 }
 NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]
 stringByAppendingPathComponent: @"ProjectManagement.sqlite"]];
 NSError *error = nil;
 persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
 initWithManagedObjectModel:[self managedObjectModel]];
 if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
 configuration:nil
 URL:storeUrl
 options:nil error:&error]){
 /*Error for store creation should be handled in here*/
 }
 return persistentStoreCoordinator;
}
- (NSManagedObjectModel *)managedObjectModel {
 if (managedObjectModel != nil) {
 return managedObjectModel;
 }
 managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
 return managedObjectModel;
}
- (NSString*)applicationDocumentsDirectory{
 return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
}
answered Jul 29, 2013 at 3:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.